Need help finding the fix for small errors

I just have a few errors with my program that I can't seem to fix. Please help me! I am compiling with unix as well.

Errors: http://imgur.com/a/QYVUJ

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;
    }
}
fix errors one at a time, by looking at each issue and then recompile as one error can spawn more spew from the compiler. Ive had 1 missing ; spurt out 20 errors or more before.

Lets take a look ...

void fillArray(char* argv[], int numbers[]); //function declerations

ok, first, argv is a special variable name used as input to main. Please, for your own sanity and mine, don't put it anywhere else. Same for argc.

second, the input from argv is ALWAYS a string. Strings cannot just be converted to integers.

so lets fix from the inside out ... fillarray is a mess right now.

void fillArray(char* a, int numbers[]) //fills array with random numbers
// style, i prefer void fillArray(char* a, int *numbers)
{
int count = atoi(a); //standard function converted string to integer
int i; //while i can be initialized to zero here, you are doing that in your loop.
//this is a style thing, its correct either way.
for (int i = 0; i<count; i++)
{
numbers[i] = rand();
}
}

Now you can fix the function header, and down in main, you can say

fillArray(argv[o]; variable);

I will also point out that argv[0] might be the program's name, it is on some systems if I remember it right? If you get a problem, check that by printing argv[0] to see what you got.





Last edited on
Do you understand what argv[0] is ? If not google it.
fillArray(argv[0]);

fillArray require more than one value....
void fillArray(char* argv[], int numbers[])

where is fork defined ?
pid = fork();

I like that you have comments, I just can't understand what your trying to do here.
1
2
3
4
5
6
7
8
9
10
11
    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
        }
    }


Maybe explain what your trying to do and what command your running to start the program.
Last edited on
Here let me post my assignment so you can understand what I'm going for.
http://imgur.com/a/grc8U

@jonnin trust me I had way more errors before this and have been sifting through them, these are the ones IDK how to take care of.

I thought argc was the name? The command line arguments I have to pass to this through unix are all numbers. The fork is defined in main, as for the loop there, trying to call said function and increase a counter based on the bool returned. My friend suggested this way to do it but I probably have it messed up...

so what I am trying to do when I execute my program is to type "a.out 123000 456 789" with 456 and 789 being the seeds for parent/child.
Last edited on
I told you how to fix the topmost error. Did you try that?
argc is the count of things typed.
argv is the things typed.

but you re-created those names in other functions and were passing them funky.

Yes...here is what errors I'm getting now...

http://imgur.com/a/ZLtD8

so 'variable' isn't declared in the scope. I tried something like what you suggested earlier with the same problem.

I think my stuffs messed a little lol

UPDATE:
http://imgur.com/a/IArRb

I tried getting rid of argv in primeCheck but it was just giving me more errors as you saw. So i just made it as it was originally, got rid of those. so now I only have these scope errors. Also I changed the use of argv[0] to argv[1] as that would be the proper size of the array. Then the parent seed is argv[2] and child seed is argv[3].
Last edited on
bump?
Only receiving a scope error for 'number' inside of int main. the function call it is contained in is "if (primeCheck(number,argv[1]))" line 35

Posting updated code...
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
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <stdio.h>
using namespace std;

bool primeCheck(int number, char* argv[]);
void fillArray(char* a, int *numbers); //function declerations

int main(int argc, char* argv[])
{
	clock_t startTime = clock();
	int numbers[*argv[1]]; //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[3]); //set its seed to child
	}
	else
	{		//else
		srand(*argv[2]); //set seed to parent
	}

	fillArray(argv[1], numbers); //fill the global array with different seeds

	for (int i = 0; i<*argv[1]; i++)
	{ //for the amount of random numbers.
		if (primeCheck(number, argv[1]))
		{	//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* a, int *numbers) //fills array with random numbers
{
	int count = atoi(a);
	int i;
	for (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;
	}

}
Last edited on
number isn't declared in main. numbers is.

so either you are trying to refer to the variable in your function "primecheck" ... you can't do that ... or you meant numbers.

while you work through these, as you see what the issue is, re-read the error text. Youll start to see what it was trying to tell you about the problem.

Last edited on
Topic archived. No new replies allowed.