architecture x86_64
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
The following code attempts to find all the prime numbers between 2 and n.
1. When compiled, why is the following error produced?:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
2. Fix code so that it compiles with no errors
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
//helper function: computes wall clock time
double getTime(struct timeval ts, struct timeval te){
doubletime = te.tv_sec - ts.tv_sec + (te.tv_usec - ts.tv_usec)/1.e6;
returntime;
}
//helper function: allocates an array of a specified length and returns a pointer
int * allocateArray(int len) {
int * result = malloc(len * sizeof(int));
returnresult;
}
//helper function: prints out elements of array separated by spaces
void printArray(int * arr, int len) {
inti;
for (i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
//helper function: checks to see if a number is prime
int isPrime(int x) {
inti;
for (i = 2; i < sqrt(x)+1; i++) { //no prime number is less than 2
if (x % i == 0) { //if the number is divisible by i
return0; //it is not prime
}
}
return1; //otherwise it is prime
}
// finds the next prime
int getNextPrime(int prev) {
intnext = prev + 1;
while (!isPrime(next)) { //while the number is not prime
next++; //increment and check again
}
returnnext;
}
// generates a sequence of primes
int genPrimeSequence(int * array, int limit) {
inti;
intlen = limit;
if (len == 0) return0;
array[0]=2; //initialize the first number to 2
for (i = 1; i < len; i++) {
array[i] = getNextPrime(array[i-1]); //fill in the array
if (array[i] > limit){
len = i;
returnlen;
}
}
returnlen;
}
int main(int argc, char ** argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s <num>\n", argv[0]);
printf("where <num> is upper limit of the desired range of primes\n");
return1;
}
structtimevaltstart, tend;
intlimit = strtol(argv[1], NULL, 10);
gettimeofday(&tstart, NULL);
int * array = allocateArray(limit); //array can't be longer than the limit size
//gettimeofday(&tend, NULL);
//printf("Time to allocate: %g\n", getTime(tstart, tend));
//gettimeofday(&tstart, NULL);
intlength = genPrimeSequence(array, limit);
gettimeofday(&tend, NULL);
printf("Time to generate primes: %g\n", getTime(tstart, tend));
printf("%d primes found.\n", length);
//printf("The first %ld prime numbers are:\n", limit);
//printArray(array, length);
return0;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education