writing an array inside the for loop with if statement

Hi Everyone!
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
#include <string>
#include <cmath>
#include <array>
#include <iostream>
//#include <cstdlib> 
#include <exception>
#include <memory>


using namespace std;

struct students {
		string name;
		int grade[5];
		double sum=0;

		 
}stu;

int main () {
int k;
	double totalAverage=0,  classAverage=0;
	char option;
	struct students student[10];
	// struct students *ptr;
	// double * ptr2=&classAverage;
	// double * ptr3=NULL;
	// ptr=student;
	student[0].name="Mehmet";
	student[1].name="Mustafa"; 
	student[2].name="Hakan";
	student[3].name="Ayşe";
	student[4].name="Ali";
	student[5].name="Ahmet";
	student[6].name="Harun";
	student[7].name="Özge";
	student[8].name="Haluk";
	student[9].name="Salih";
	student[0].grade[0]=96;	 student[1].grade[0]=76; student[2].grade[0]=54; 
	student[0].grade[1]=45;  student[1].grade[1]=85; student[2].grade[1]=70; 
	student[0].grade[2]=90;  student[1].grade[2]=96; student[2].grade[2]=70; 
	student[0].grade[3]=70;  student[1].grade[3]=80; student[2].grade[3]=90; 
	student[0].grade[4]=85;  student[1].grade[4]=65; student[2].grade[4]=65; 
	
	student[3].grade[0]=85;  student[4].grade[0]=100; student[5].grade[0]=96;	
	student[3].grade[1]=90;  student[4].grade[1]=100; student[5].grade[1]=76; 
	student[3].grade[2]=90;  student[4].grade[2]=100; student[5].grade[2]=54; 
	student[3].grade[3]=95;  student[4].grade[3]=90;  student[5].grade[3]=40; 
	student[3].grade[4]=100; student[4].grade[4]=85;  student[5].grade[4]=100;
	
	student[6].grade[0]=45; student[7].grade[0]=85; student[8].grade[0]=65;  student[9].grade[0]=85;
	student[6].grade[1]=86; student[7].grade[1]=70; student[8].grade[1]=54;  student[9].grade[1]=90;
	student[6].grade[2]=70; student[7].grade[2]=90; student[8].grade[2]=75;  student[9].grade[2]=90;
	student[6].grade[3]=95; student[7].grade[3]=80; student[8].grade[3]=54;  student[9].grade[3]=95;
	student[6].grade[4]=75; student[7].grade[4]=85; student[8].grade[4]=70;  student[9].grade[4]=100;

//----------------------
//I did not write here the remaining codes
//----------------------

	cout<<" Toplam Ortalama: ";
	 	for (k=0; k<5; ++k)
		{
			student[0].sum+=student[0].grade[k];
			student[1].sum+=student[1].grade[k];
			student[2].sum+=student[2].grade[k];
			student[3].sum+=student[3].grade[k];
			student[4].sum+=student[4].grade[k];
			student[5].sum+=student[5].grade[k];
			student[6].sum+=student[6].grade[k];
			student[7].sum+=student[7].grade[k];
			student[8].sum+=student[8].grade[k];
			student[9].sum+=student[9].grade[k];
			totalAverage=(student[0].sum/5)+ 
(student[1].sum/5)+student[2].sum/5+student[3].sum/5+student[4].sum/5+student[5].sum/5
+student[6].sum/5+student[7].sum/5+student[8].sum/5+student[9].sum/5;
			                                            
		
        } 
 cout<<totalAverage<<"   ";
 classAverage=totalAverage/10;
  cout<<classAverage<<"   ";
}

-------------------

how do i write the following code using less code. This code working
1
2
3
4
5
6
7
8
student[0].sum+=student[0].grade[k];,
student[0].sum+=student[0].grade[k];
student[1].sum+=student[1].grade[k];
-
-
student[9].sum+=student[9].grade[k];
totalAverage=(student[0].sum/5)+(student[1].sum/5)+student[2].sum/5+student[3].sum/5+student[4].sum/5+student[5].sum/5
+student[6].sum/5+student[7].sum/5+student[8].sum/5+student[9].sum/5;

-------------------
I tried the following but totalAverage is false calculating by cpu
-------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
for (i=0; i<10; i++) 
		{	
		for (k=0; k<5; ++k)                               
		  			
			{ 
		
			student[i].sum+=student[i].grade[k];
			cout<<"Öğrenci: " << student[i].name<<" "<<student[i].grade[k]<<endl;
			totalAverage+=(student[i].sum)/5;
			cout<<"Total Ortalama: "<<totalAverage<<"  ";
		
		}
		}  

-------------------
I want the code the following way
student[0].grade[0]
student[0].grade[1]
student[0].grade[2]
student[0].grade[3]
_
_
student[3].grade[0]
_
_
student[3].grade[3]
-------------------
but computer is showing an array the following way
-------------------
student[0].grade[0]
student[1].grade[0]
student[2].grade[0]
student[3].grade[0]
_
_
student[9].grade[0]

Last edited on
The format tools did not work when first new topic
> totalAverage+=(student[i].sum)/5;
This needs to be outside your for(k loop, but inside your for(i loop.
@salem c
> totalAverage+=(student[i].sum)/5;
This needs to be outside your for(k loop, but inside your for(i loop.


Thanks It's working

I have more of a problem
when I reload(execute) it without closing, value of variable(average, classAverage totalAverage ) is increasing

I am using this code for reload(execute) it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
secim1:cout<<endl<<"Başka işlem yapmak ister misiniz? Evet için 1, Hayır için 2 tuşlayınız. ";
	cout<<endl<<"Seçiminiz: ";
	cin>>option;
	if (option=='1' || option=='2')
	{
	switch (option)
	{
		case '1':
		goto liste;
	    case '2':
		exit(0);
	}
	}
	else
	{
		exit(0);
	}

This is second loading it is result
Total Ortalama(totalAverage): 1598.8  Sınıf Ortalaması(classAverage): 160


I used windows cmd

Last edited on
> when I reload(execute) it without closing, value of variable(average, classAverage totalAverage ) is increasing
That's because you have a bloated main() function containing all your code, where your variables are only initialised once, and you're using goto to jump all over the place like a whirling dervish.

Add some structure to your 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <string>
using namespace std;

const int NUM_STUDENTS = 10;
const int NUM_GRADES = 5;

// This is one student, so the name is singular
struct student {
  string name;
  int grade[NUM_GRADES];
  double sum;
};

void initialise(student students[])
{
  students[0].name = "Mehmet";
  students[1].name = "Mustafa";
  students[2].name = "Hakan";
  students[3].name = "Ayşe";
  students[4].name = "Ali";
  students[5].name = "Ahmet";
  students[6].name = "Harun";
  students[7].name = "Özge";
  students[8].name = "Haluk";
  students[9].name = "Salih";

  //!! this could be ordered better to
  //!! see whether every grade for every student
  //!! is set up properly
  students[0].grade[0] = 96;
  students[1].grade[0] = 76;
  students[2].grade[0] = 54;
  students[0].grade[1] = 45;
  students[1].grade[1] = 85;
  students[2].grade[1] = 70;
  students[0].grade[2] = 90;
  students[1].grade[2] = 96;
  students[2].grade[2] = 70;
  students[0].grade[3] = 70;
  students[1].grade[3] = 80;
  students[2].grade[3] = 90;
  students[0].grade[4] = 85;
  students[1].grade[4] = 65;
  students[2].grade[4] = 65;
  students[3].grade[0] = 85;
  students[4].grade[0] = 100;
  students[5].grade[0] = 96;
  students[3].grade[1] = 90;
  students[4].grade[1] = 100;
  students[5].grade[1] = 76;
  students[3].grade[2] = 90;
  students[4].grade[2] = 100;
  students[5].grade[2] = 54;
  students[3].grade[3] = 95;
  students[4].grade[3] = 90;
  students[5].grade[3] = 40;
  students[3].grade[4] = 100;
  students[4].grade[4] = 85;
  students[5].grade[4] = 100;
  students[6].grade[0] = 45;
  students[7].grade[0] = 85;
  students[8].grade[0] = 65;
  students[9].grade[0] = 85;
  students[6].grade[1] = 86;
  students[7].grade[1] = 70;
  students[8].grade[1] = 54;
  students[9].grade[1] = 90;
  students[6].grade[2] = 70;
  students[7].grade[2] = 90;
  students[8].grade[2] = 75;
  students[9].grade[2] = 90;
  students[6].grade[3] = 95;
  students[7].grade[3] = 80;
  students[8].grade[3] = 54;
  students[9].grade[3] = 95;
  students[6].grade[4] = 75;
  students[7].grade[4] = 85;
  students[8].grade[4] = 70;
  students[9].grade[4] = 100;
}

//!! your averages are automatically re-initialised to 0
//!! every time you call the function.
void calculate(student students[])
{
  double totalAverage = 0, classAverage = 0;
  for (int i = 0; i < NUM_STUDENTS; i++) {
    students[i].sum = 0;
    for (int k = 0; k < NUM_GRADES; ++k) {
      students[i].sum += students[i].grade[k];
    }
    totalAverage += (students[i].sum) / NUM_GRADES;
  }
  cout << totalAverage << "   ";
  classAverage = totalAverage / NUM_STUDENTS;
  cout << classAverage << "   ";
}

int main()
{
  student students[NUM_STUDENTS];
  initialise(students);
  char option;
  do {
    cout<<endl<<"Başka işlem yapmak ister misiniz? Evet için 1, Hayır için 2 tuşlayınız. ";
    cout<<endl<<"Seçiminiz: ";
    cin>>option;
    if ( option == '1' ) {
        calculate(students);
    }
  } while ( option != '2' );
  return 0;
}
Topic archived. No new replies allowed.