How to implement stack

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
/* Fig. 3.10: fig03_10.c
   Nick Vincent
   Analysis of examination results */

#include <stdio.h>



int main()
{
	/* initializing variables in declarations */
	int passes = 0, failures = 0, student = 0, result =0;

	/* process 10 students; counter-controlled loop */
	while ( student <= 10 ) 
	{
		printf( "Enter result ( 1 = pass, 2 = fail ): " );
		scanf( "%d", &result );

		if ( result == 1 ) {       /* if/else nested in while */
			passes = (passes + 1);}
		else
		{	failures = (failures + 1);

		student = (student + 1);
		result = 0;}
	}

	printf( "Passed %d\n", passes );
	printf( "Failed %d\n", failures );

	if ( passes > 8 )
		printf( "Raise tuition\n" );

	getchar();
	getchar();
	return 0;   /* successful termination */
}


I have written the code above from my book. I need some instruction in the right direction on how to implement the "struct stack" idea that makes the program do the same thing. I have no idea on where to start besides declaring the "struct stack;" Any information would be great.
What is a "stuct stack" idea?

And btw: You may recheck whether you made a typo in the else-clause of the if in line 23. The closing bracket is in line 26, hence the student is only increment for failures.


Ciao, Imi.
Bazzy: Yes, that is a description of a stack data structure. My question was a rather rhetorical shortcut for "What has the data structure of a stack to do with your program at hand and are you aware by yourself what you actually want to achive?"

Ciao, Imi.
I suppose you want this to be implemented.

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
#include <stdio.h>


int _tmain(int argc, _TCHAR* argv[])
{
		/* initializing variables in declarations */
	int result =0;
	struct _Performance
	{
		int passes ; 
		int failures;
		int students;
	} performance = {0};


	/* process 10 students; counter-controlled loop */
	while ( performance.students <= 10 ) 
	{
		printf( "Enter result ( 1 = pass, 2 = fail ): " );
		scanf( "%d", &result );

		if ( result == 1 ) 
			{       /* if/else nested in while */
				performance.passes++;
			}
		else
		{	
			performance.failures ++;
		}
			performance.students++;
			result = 0;
	}

	printf( "Passed %d\n", performance.passes );
	printf( "Failed %d\n", performance.failures );

	if ( performance.passes > 8 )
		printf( "Raise tuition\n" );

	getchar();
	getchar();

	return 0;
}
@imi my link was for the OP
Topic archived. No new replies allowed.