These small errors are driving me crazy

I am compiling with unix and getting these Errors: http://imgur.com/a/QYVUJ
I can not find out how to fix these, I've tried a few good ways
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <stdio.h>
using namespace std;
 
bool primeCheck(int, char* argv[]);
void fillArray(char* argv[], int numbers[]); //function declerations
 
 
 
int main(int argc, char* argv[])
{
    clock_t startTime = clock();
    int numbers[*argv[0]]; //this is the global array for the random numbers
    int prime = 0; //these two are used for storing the amount of primes/not primes
    int notPrime = 0;
 
    int pid; //this will be filled with the process id for the child
    pid = fork(); //the pid is here assigned and a new process is started
 
    if (getpid() == pid)
    { //if this is the child
        srand(*argv[2]); //set its seed to child
    }
    else
    {       //else
        srand(*argv[1]); //set seed to parent
    }
 
    fillArray(*argv[0]); //fill the global array with different seeds
 
    for (int i = 0; i<*argv[0]; i++)
    { //for the amount of random numbers.
        if (primeCheck())
        {   //check if its prime
            prime++;    //if its prime, increase it
        }
        else
        {       //else
            notPrime++; //count up not prime
        }
    }
 
 
    clock_t endTime = clock();
    clock_t clockTicks = endTime - startTime;
    double timeSec = clockTicks / (double)CLOCKS_PER_SEC;
 
    if (pid == 0)
    {
        // child
        cout << "C; ID: " << getpid() << " Prime: " << prime << " Not Prime: " << notPrime << "CPU Time:  " << timeSec << endl;
        exit(1);
    }
 
    else
    {      //parent
 
        cout << "P; ID: " << getpid() << " Prime: " << prime << " Not Prime: " << notPrime << "CPU Time: " << timeSec << endl;
 
    }
 
    return 0;
}
 
void fillArray(char* argv[], int numbers[]) //fills array with random numbers
{
    int count = *argv[0];
    int i = 0;
    for (int i = 0; i<count; i++)
    {
        numbers[i] = rand();
    }
}
 
bool primeCheck(int number, char* argv[]) //goes through array to check numbers
{
    int i = 2;
 
    while (i < (number / 2))
    {
        if (number % i == 0)
        {
 
            return false;
        }
        ++i;
    }
 
    if (i == (number / 2))
    {
        return true;
    }
}
First off, please don't double-post. I know this is a bit of a gray area since there are some Unix specific ideas, but your main problem is not the Unix code but instead c++ basics.

If you look into what the argv[] in main(int argc, char * argv[]) actually is then you can rewrite your code to make more sense. Especially you need to look into parsing from a char string into an int. A quick google search should get you there. Perhaps also research a bit deeper into pointers and arrays.

There's enough conceptually wrong with this code that you might need to start over from scratch once that research is done, that might be the easiest way. If you choose that route then test your code as often as possible to catch errors before they become intrinsic in the code.

People in the beginner's forums won't steer you wrong, and there is a much larger pool of people in those forums.

Good luck.
Last edited on
Topic archived. No new replies allowed.