calling a function inside another function

Hey guys im facing an issue where im trying to make a chart that is only prime numbers and im having an issue where its displaying all the numbers.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  #include<iostream>
#include<iomanip>
using namespace std;

// Function prototype
//       - Name: isPrime
//         Parameters:
//         - 1 double for the number you are trying to determine "primeness" (pass by value)
//         Returns: a bool - true if number is prime, false otherwise

bool isPrime(int);

// TODO: Declare another function prototype
//       - Name: PrimeList
//         Parameters:
//         - 1 int for how high you want your list of primes to go
//         Returns: <nothing>
void PrimeList(int listMax);


// ********************************************
int main()
{
	// Declare program variables.
	int listMax;


	// Prompt the user to enter a number and read their answer
	do {
		cout << "Display a list of primes up to (enter + number < " << INT_MAX << "): ";
		cin >> listMax;
	} while (listMax <= 0 || listMax > INT_MAX);     // the number to test must be greater than 0

	// TODO: Call function to determine if the user's number is a prime
	// Pass one argument representing the number to be tested
	// Assign the returned value to the correct variable
	PrimeList(listMax);


	cout << endl << endl;
	cout << "End program - ";
	return 0;
}

// ********************************************
// TODO: Define isPrime()
//
// Replace this implementation of IsPrime with your implementation from Lab 28
// You may keep this version for testing if your Lab 28 didn't work, but
// every number will be designated as a prime.

bool isPrime(int numberToTest)
{
	for (int a = 2; a < numberToTest; a++)
	{
		if (numberToTest % a == 0)
		{
			return false;
		}
	}
	return true;
}

// ********************************************
// TODO: Define PrimeList()
// Refer to the function prototype above for parameter and return type info
//
// Algorithm Pseudocode:
//
// Loop from 2 to the max number for the list
// If the current loop counter is a prime number (call the IsPrime function to find out)
//    Print the number to the screen (in an 8 character column)
void PrimeList(int listMax)
{
	int column = 1;
	int row = 1;
	
	while (row <= listMax)
	{
		bool isPrime(int);
		while (column <= listMax)
		{
			cout << setw(4) << row * column;
			column++;
		}
		row++;
		cout << endl;
	}
	
}


/* Sample Output
Display a list of primes up to (enter + number < 2147483647): 1000
       3       5       7      11      13      17      19
      23      29      31      37      41      43      47
      53      59      61      67      71      73      79
      83      89      97     101     103     107     109
	 113     127     131     137     139     149     151
     157     163     167     173     179     181     191
	 193     197     199     211     223     227     229
	 233     239     241     251     257     263     269
	 271     277     281     283     293     307     311
	 313     317     331     337     347     349     353
	 359     367     373     379     383     389     397
	 401     409     419     421     431     433     439
	 443     449     457     461     463     467     479
	 487     491     499     503     509     521     523
	 541     547     557     563     569     571     577
	 587     593     599     601     607     613     617
	 619     631     641     643     647     653     659
	 661     673     677     683     691     701     709
	 719     727     733     739     743     751     757
	 761     769     773     787     797     809     811
	 821     823     827     829     839     853     857
	 859     863     877     881     883     887     907
	 911     919     929     937     941     947     953
     967     971     977     983     991     997 */
¿what do you think line 80 is doing?
I was guessing with line 80 that im calling the function inside the function PrimeList so that it can display only prime numbers.
your function ask for a parameter, the number what will be tested for primality.
¿what number are you passing to your function as parameter?
bool isPrime(int);
so i tried using listMax considering that listMax is used for what the user enters in and im still not getting prime numbers? i kinda feel clueless with this program a bit. any other ways you can help? wanna make sure i understand this material.
anything guys?
isPrime() is a function. It has an input and an output.

Look at the function definition:
1
2
3
4
5
6
7
8
9
10
11
bool isPrime(int numberToTest)
{
    for (int a = 2; a < numberToTest; a++)
    {
        if (numberToTest % a == 0)
        {
            return false;
        }
    }
    return true;
}

numberToTest is the input. The function does something with that (right now, we don't need to know what it actually does), and it returns a result - see lines 7 and 10, it returns a true or false value. That makes sense, because the declaration bool isPrime(...) etc. tells us that it will return a value of type bool.

Now, when you call the function, you need to do two things.
1. pass the value you want to test as the input parameter.
2. do something with the returned result.

Typically you might assign the result to another variable (though that merely defers the problem here) or test it in an if statement.

For example, you might do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
    for (int n=1; n<=listMax; ++n)
    {
        cout << setw(4) << n;
        if (isPrime(n))
        {
            cout << " prime\n";
        }
        else
        {
            cout << " not prime\n";
        }
        
    }

Here, I've passed the value n as the input and tested the result in an if statement.

In your code, I'm not quite sure why you have two while loops (in function PrimeList), currently, the number you are printing out is row * column, so that is the value to be passed to the function isPrime().
Last edited on
i gave it a try but it seems like im getting an infinite loop for some reason?

so here is an update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void PrimeList(int listMax)
{
	int row = 1;
	int column = 1;
	const int MAX = 8;
	while (row <= MAX)
	{
		column = 1;

		while (column <= MAX)
		{
			cout << isPrime(listMax) << setw(4) << row * column;
			column++;
		}

		row++;
		cout << endl;
	}
}


My number seems to be off it goes up by 10s instead of the prime numbers?
Last edited on
@undecked0 I tried running your latest code.
I called it like this from main(), PrimeList(30);
and this was the output:
0   10   20   30   40   50   60   70   8
0   20   40   60   80  100  120  140  16
0   30   60   90  120  150  180  210  24
0   40   80  120  160  200  240  280  32
0   50  100  150  200  250  300  350  40
0   60  120  180  240  300  360  420  48
0   70  140  210  280  350  420  490  56
0   80  160  240  320  400  480  560  64


Now if I run it like this PrimeList(31);, then this is the output:
1   11   21   31   41   51   61   71   8
1   21   41   61   81  101  121  141  16
1   31   61   91  121  151  181  211  24
1   41   81  121  161  201  241  281  32
1   51  101  151  201  251  301  351  40
1   61  121  181  241  301  361  421  48
1   71  141  211  281  351  421  491  56
1   81  161  241  321  401  481  561  64


There's a pattern there, but it may not yet be clear.
So let's add a single extra line at the start,
1
2
3
4
5
6
void PrimeList(int listMax)
{
    cout << std::boolalpha;
    
    // etc.
    // rest of code unchanged 


Now this is the output:
true   1true   2true   3true   4true   5true   6true   7true   8
true   2true   4true   6true   8true  10true  12true  14true  16
true   3true   6true   9true  12true  15true  18true  21true  24
true   4true   8true  12true  16true  20true  24true  28true  32
true   5true  10true  15true  20true  25true  30true  35true  40
true   6true  12true  18true  24true  30true  36true  42true  48
true   7true  14true  21true  28true  35true  42true  49true  56
true   8true  16true  24true  32true  40true  48true  56true  64


My apologies for going through this so slowly - one step at a time and we'll start to be able to explain what is happening.

You see that word "true" or "false" which appears? That is the output from the function isPrime(listMax). The result is a boolean value (either true or false). By default it is shown as the digit 1 or 0. The boolalpha manipulator is used, it sets a flag so that the text rather than the digit is shown.

But why is it always the same value (all true or all false)? Well, the value listMax doesn't change, so the result will always be the same.


If we go back to the post I made yesterday, perhaps you missed the salient points. In your code, I suggested two things:
1. call the function isPrime with the value row * column.
2. use an if statement to test the result.
That would lead to code something like this:
1
2
    if (isPrime(row * column))
        cout << setw(4) << row * column;

Now if that is dropped into your existing code, the output looks a bit like this:
   1   2   3   5   7
   2
   3

   5

   7

Now there is something positive there. The numbers 2 3 5 and 7 are indeed prime numbers. 1 is not considered prime, a small tweak to function isPrime() can fix that.

As for your output, it has a few problems. The really important one is that the value listMax is not used at all, anywhere within the function.

I'd start by modifying the function to just step through every integer from 1 to listMax. Only after that would I begin to consider how to format the output into columns.
1
2
3
4
5
6
7
8
9
10
11
12
13
void PrimeList(int listMax) 
{    
    int number = 1;

    while (number <= listMax)
    {
        if (isPrime(number))
        {
            cout << setw(4) << number;
        }
        number++;
    }
}

Output:
   1   2   3   5   7  11  13  17  19  23  29

Last edited on
Thank you so much so now it looks like im getting my prime numbers now all i got to do is have it looking like the table in the sample output

Here is the current coding
1
2
3
4
5
6
7
8
9
10
11
12
13
void PrimeList(int listMax)
{
	int number = 3;

	while (number <= listMax)
	{
		if (isPrime(number))
		{
			cout << setw(4) << number;
		}
		number++;
	}
}


my question is now, can i put in the row * column into the PrimeList function?
my question is now, can i put in the row * column into the PrimeList function?

Well, I would not worry about row. The number of rows will depend on the value of listMax - but there isn't a simple calculation you can do to determine in advance how many rows there will be.

Instead, just consider the columns.

One possible way. Each time you output a value at line 9, you could add 1 to a column number. When that column number reaches your required value of 8, output a newline, and reset the column count to zero.

By the way, here int number = 3;, you are missing the first prime number which should be 2.

oops was messing around with the number = 3; also anyway to shine some light for me on how to do the columns? just a hint, sorry i have a complete brain fart from this assignment.
Well, I already described what you need to do. Define an integer variable named column. Give it an initial value of zero.

The rest is in my previous post,
Chervil wrote:
Each time you output a value at line 9, you could add 1 to a column number. When that column number reaches your required value of 8, output a newline, and reset the column count to zero.


Topic archived. No new replies allowed.