architecture x86_64

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter8: Advanced Method Concepts
Section: Chapter Questions
Problem 7RQ
icon
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
 
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Fundamentals of Computer System
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,