First post on the forum, so go easy on me... I did the warning threads and I hope i'm not violating any forum etiquette by asking for some homework help.

So, the gist:

1st year in university, intro to Comp-Sci. first homework assignment.

Problem statement:

Run a survey on the students in your class, ask them if they prefer:

1) To study in the morning (m) or the evening (e)

2) To study alone (a) or in a group (g)

3) To study with music (m) or in silence (s)

than, print out the percentage of every preference.

the output should look like this:

Code: Select all

`How many students will participate in the survey? `

3

Student 1:

1)Do you study in the morning (m) or in the evening (e)?

m

2)Do you study alone (a) or with a group (g)?

a

3)Do you study with music (m) or prefer silence (s)?

s

Student 2:

1)Do you study in the morning (m) or in the evening (e)?

e

2)Do you study alone (a) or with a group (g)?

a

3)Do you study with music (m) or prefer silence (s)?

m

Student 3:

1)Do you study in the morning (m) or in the evening (e)?

m

2)Do you study alone (a) or with a group (g)?

a

3)Do you study with music (m) or prefer silence (s)?

m

================================================================================================

The results are:

From a group of 3 students:

66.67% of the students study in the morning and 33.33 % in the eveni

100.00% of the students study alone and 0.00% in group.

66.67% of the students study with music and 33.33% in silence.

I've attached my code, which compiles and runs without crashing (woo-hoo! finally) but seems to have some errors:

1) Every OTHER question (as in, one in two) doesn't accept an answer.

2) the percentages at the end are all zero

I would REALLY appreciate anyone giving any sort of advice (including persuading me to drop this and go play in traffic...)

Thanks in advance!

CODE:

Code: Select all

`#include <stdio.h>`

int main()

{

int i, num_students, num_morning, num_e, num_a, num_g, num_music, num_s;

double perc_morning, perc_e, perc_a, perc_g, perc_music, perc_s;

char study_time, study_together, study_music;

num_students= num_morning= num_e= num_a= num_g= num_music= num_s= 0;

printf("How many students will participate in the survey? \n");

scanf("%d", &num_students);

for (i=0; i<num_students; i++){

//Question 1

printf("1)Do you study in the morning (m) or in the evening (e)? \n");

scanf("%c", &study_time);

if (study_time == 'm'){

num_morning++;

}

if (study_time == 'e') {

num_e++;

}

if (study_time != 'e' && study_time != 'm'){

printf("Your answer is not valid and was not counted \n");

}

//Question 2

printf("2)Do you study alone (a) or with a group (g)? \n");

scanf("%c", &study_together);

if (study_together == 'a'){

num_a++;

}

if (study_together == 'g') {

num_g++;

}

if (study_together != 'a' && study_together != 'g'){

printf("Your answer is not valid and was not counted \n");

}

//Question 3

printf("3)Do you study with music (m) or prefer silence (s)? \n");

scanf("%c", &study_music);

if (study_music == 'm'){

num_music++;

}

if (study_music == 's') {

num_s++;

}

if (study_music != 'm' && study_music != 's'){

printf("Your answer is not valid and was not counted \n");

}

}

perc_morning = num_morning / num_students * 100;

perc_e = num_e / num_students * 100;

perc_a = num_a / num_students * 100;

perc_g = num_g / num_students * 100;

perc_music = num_music / num_students * 100;

perc_s = num_s / num_students * 100;

printf("=======================================================\n");

printf("The results are: \n From a group of %d students: \n", num_students);

printf("%lf of the students study in the morning and %lf%% in the evening \n", perc_morning, perc_e);

printf("%lf of the students study alone and %lf%% in a group \n", perc_a, perc_g);

printf("%lf of the students study with music and %lf%% in silence", perc_music, perc_s);

return 0;

}