Wondering if someone could guide me in the right direction. I think this code is right but can't figure out why my switch loop won't access my char array of grades and add it to an int array.

#include <stdio.h>

int main (void)

{

	char grade[32] = {
						'A', 'B', 'C', 'D', 'f', 'I',
						'b', 'C', 'd', 'F', 'I', 'a',
						'C', 'D', 'F', 'I', 'A', 'B',
						'D', 'F', 'I', 'A', 'B', 'c',
						'I', 'D', 'F', 'F', 'A', 'B',
						'a', 'b'};

	char letters[6] = { 'A', 'B', 'C', 'D', 'F', 'I'};
						
	
	int i, hist_grade[6];

	for (i = 0; i < 32; ++i)
		grade[i] = 0;
	{
		switch(grade[32])
		{
		case'A':
		case'a':
		++hist_grade[i];
		break;

		case 'B': 
		case 'b':
		++hist_grade[i];
		break;

		case 'C': 
		case 'c':
		++hist_grade[i];
		break;
		
		case'D':
		case'd':
		++hist_grade[i];
		break;

		case'F':
		case'f':
		++hist_grade[i];
		break;

		case 'I':
		case 'i':
		++hist_grade[i];
		break;

		}

	}

	printf ("\n\nGrade	Number of Each Grade\n");
	printf ("-----	--------------------\n");

	for (i = 0; i < 6; ++i)
		printf ("%c%8i\n", letters[i] , hist_grade[i]);

	
	getchar();
	return 0;
}

I think if you look closely, you'll find that your switch statement is outside your loop.

This is your for loop

for (i = 0; i < 32; ++i)
		grade[i] = 0;

To include the switch you'll need to add braces like

for (i = 0; i < 32; ++i)
{
grade[i] = 0;
switch(something)
{...}
}

Not sure that was the problem. I compiled again with the the grade = 0 inside the braces and I get the same exact out put.

line 22 destroys the value of all the grades. Delete that line

line 24: only grade[32] is used in that switch statement. And grade[32] is wrong because it accesses an element of grade array beyond the bounds of the array. In orderwords, there is no grade[32] element. Elements are numbered 0 to 31.

What I think you want there is switch( grade[i] )

Before For loop initialize all elements of array hist_grade[6] to 0.

line 22: why are you destroying all values of grade to 0.. can't get the need of this line..
switch statement should me inside for loop..

line 24:

switch(grade[32])

should'nt it be

switch(grade[i])

line 28: how can you use it..

++hist_grade[i];

The values of i varies from 0 to 31 and hist_grade[6] can hold 0 to 5..
It should be

++hist_grade[0];

Line 33: ++hist_grade[1];

Line 38: ++hist_grade[2]; and so on...

Thanks guys... that was a huge help.

On a side note, what do you think about the Visual C++ Express compiler.

Say tuned for problems with printing 2D arrays up next :)

Thanks again.

>>On a side note, what do you think about the Visual C++ Express compiler.

Best one on the market for MS-Windows operating system, although more difficult to learn than Code::Blocks because if it's many whistles and bells.

what result is this supposed to print? i got a similar assignment and i'm not sure if the output is what i should expect...

Grade Number of Each Grade
----- --------------------
A1076285190
B1074806778
C1074792700
D1073831181
F-16121850
I-1079501539

#include <stdio.h>

int main (void)

{

	char grade[32] = {
						'A', 'B', 'C', 'D', 'f', 'I',
						'b', 'C', 'd', 'F', 'I', 'a',
						'C', 'D', 'F', 'I', 'A', 'B',
						'D', 'F', 'I', 'A', 'B', 'c',
						'I', 'D', 'F', 'F', 'A', 'B',
						'a', 'b'};

	char letters[6] = { 'A', 'B', 'C', 'D', 'F', 'I'};
						
	
	int i;
        int hist_grade[6] = {0,0,0,0,0,0};

	for (i = 0; i < 32; ++i)
         {
		
              switch(grade[i])
		{
		case'A':
		case'a':
		hist_grade[0]++;
		break;

		case 'B': 
		case 'b':
		hist_grade[1]++;
		break;

		case 'C': 
		case 'c':
		hist_grade[2]++;
		break;
		
		case'D':
		case'd':
		hist_grade[3]++;
		break;

		case'F':
		case'f':
		hist_grade[4]++;
		break;

		case 'I':
		case 'i':
		hist_grade[5]++;
		break;

		}

	}

	printf ("\n\nGrade	Number of Each Grade\n");
	printf ("-----	--------------------\n");

	for (i = 0; i < 6; i++)
		printf ("%c%8d\n", letters[i] , hist_grade[i]);

	
	return 0;
}

output:

Grade	Number of Each Grade
-----	--------------------
A       6
B       6
C       4
D       5
F       6
I       5
commented: Do NOT write homework for others!!! -4

LOL, now that makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.