wrong output

cant someone tell me what wrong with this code?

the frequent answer didn't match with the result

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

using namespace std;

int main()

{

int num[7];


cout<<"answer all question.1,2,or 3"<<endl<<endl;


cout<<"QUESTION 1"<<endl<<endl;

cout<<"A)your favourate food?"<<endl<<endl;
cout<<"1)chicken"<<endl;
cout<<"2)fish"<<endl;
cout<<"3)meat"<<endl<<endl;

cin >> num[0]; 



cout<<"QUESTION 2"<<endl<<endl;

cout<<"A)your favourate drinks?<<endl"<<endl;
cout<<"1)coca-cola"<<endl;
cout<<"2)sprite"<<endl;
cout<<"3)pepsi"<<endl<<endl;

cin >> num[1]; 



cout<<"QUESTION 3"<<endl<<endl;

cout<<"A)your favourate football team?"<<endl<<endl;
cout<<"1)brazil"<<endl;
cout<<"2)england"<<endl;
cout<<"3)argentina"<<endl<<endl;

cin >> num[2]; 



cout<<"QUESTION 4"<<endl<<endl;

cout<<"A)your favourate ice-cream flavour?"<<endl<<endl;
cout<<"1)vanila"<<endl;
cout<<"2)chocolate"<<endl;
cout<<"3)strawberry"<<endl<<endl;

cin >> num[3]; 


cout<<"QUESTION 5"<<endl<<endl;

cout<<"A)your favourate book?"<<endl<<endl;
cout<<"1)novel"<<endl;
cout<<"2)magazine"<<endl;
cout<<"3)education"<<endl<<endl;

cin >> num[4]; 

cout<<"QUESTION 6"<<endl<<endl;

cout<<"A)your favourate transportation?"<<endl<<endl;
cout<<"1)car"<<endl;
cout<<"2)train"<<endl;
cout<<"3)motocycle"<<endl<<endl;

cin >> num[5];

cout<<"QUESTION 7"<<endl<<endl;

cout<<"A)your favourate color?"<<endl<<endl;
cout<<"1)red"<<endl;
cout<<"2)white"<<endl;
cout<<"3)blue"<<endl<<endl;

cin >> num[6];  

cout<<"your frequent answer "<<endl;


int sorted[7];
int N = sizeof(num)/sizeof(num[0]);
  int last_change=0;
  
for (int i=1; i<4; ++i){ 
    int n;
    for (n = last_change; n<N && num[n]==i; ++n) {
}
    cout << i << " repeted " << (n-last_change) << " times " <<endl;
	last_change = n;

}
	cout<<"thank you"<<endl<<endl;
system("pause");
return 0;
}
I have'nt 'played about' with sorting yet, But it would be easier in a program like this to have 3 variable's that get incremented dependinding on the answer.

Line 28 cout<<"A)your favourate drinks?<<endl"<<endl;

should be: cout<<"A)your favourate drinks?"<<endl<<endl;
This is a straight forward adding up exercise, you're inclusion of N,last_change and sorted just add to the confusion.

Also, it looks like your code is looking for the longest run of the same answer, which is different to the most frequent answer. I have presumed that your text is right and your code wrong ;)


1
2
3
4
5
6
7
8
9
10
11
12
    for (int j = 1; j<=3; j++) // examine each possible answer
    {
        int cnt = 0;                   // tally for this answer
        for (int k=0; k<7; k++) // iterate the questions looking for this answer
        {
             if (num[k]==j)          // when we find it, increment the tally
                 cnt++;
        }
        cout <<j << " repeated " << (cnt) << " times " <<endl;

    }
    cout<<"thank you"<<endl<<endl;
Topic archived. No new replies allowed.