need immediate help pls?

im doing a console program.
can you figure out what's wrong with this code??
how can i make it re-execute the program at runtime.

the program prompts the user to assign a value for x.
x should be less than m. where m = 8.
when the input is greater than m.
the program the program will prompt for another value that shuold be less than m.
here's the formula.
x = (x*a+c)mod m

it's involves recursion.
when f is already equal to x. recursion stops.


here's the code:(it's not yet finished.)

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
#include <iostream>

using std::cout;
using std::cin;
using std::endl;


void generate(int x)
{
	int m, a, c, p1, p2, f, n, d;

	m = 8;
	if (x < m)
	{
	a = 5;
	c = 7;

	p1 = x * a;
	p2 = p1 + c;
	f = p2 % m;
	n = 0;

	printf("\t%d\t%d\t%d\t%d\t%d\t\n",n,x,p1,p2,f);

		do
		{
		d = f;
		p1 = d * a;
		p2 = p1 + c;
		f = p2 % m;
		n = n++;

		printf("\t%d\t%d\t%d\t%d\t%d\t\n",n,d,p1,p2,f);
		}while(x <= f);
	}
	else
	{
		cout<<"x should be less than m";
	}

}

void main()
{
	int x;

	cout<< "Assign Value for x: ";

	cin>> x;

	printf("\tn\tx\tp1\tp2\tf\n\n");

	generate(x);
}


thanks.
Last edited on
Hi Jaymz,

There are a few basic problems with the code (although probably NOT the problems that you actually want us to help with!)

First is that void main() is non-standard C++ and you really SHOULD be using int main() (or even preferably int main(int argc, char *argv[])). In which case you need to include return 0; in main().

Second, there are none of those lovely comments which are supposed to be there to help other people make sense of what you're trying to do.

Third, you've got a curious mix of C and C++ code in there -- why precisely are you using both cout and printf()?

May I suggest that you fix these and see if it doesn't help? This may seem pointless, but believe me when I say if you insert CLEAR comments which indicate when you are entering a loop, checking an exit condition, etc, then you will see your problem -- a quick look at the section of the function generate(x) below should serve to demonstrate both why your program isn't working as you want AND the advantage of clear commenting;

1
2
3
4
5
6
7
8
9
10
11
12

        int m, a, c, p1, p2, f, n, d;

	m = 8;

        // if else loop
        // proceeds if (x<m), else prints error message and exits generate(x)   
	if (x < m)       
	{
	a = 5;
	c = 7;


But you don't WANT to exit if (x<m) is false, right?

For what it's worth -- I usually start small programs like this by writing comments explaining what I'm trying to achieve;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

        // prompt user for an input. if input is out of range, prompt again (need
        // an exit condition, though). if input is within range, then perform
        // calculation

        // top of input loop - use cin to get input, use "q" as exit condition 
        // both "q" and valid input will terminate loop - but only valid input
        // will call the calculation 
        
             // if input valid perform calculation (and terminate loop)

             // if input invalid show error message

             // if input "q" do nothing (and terminate loop)

        // bottom of input loop

        // And so on...


All I then need to do is slip in the relevant code under each heading.

Hope it helps in more than one way!

Regards,
muzhogg


Last edited on
Topic archived. No new replies allowed.