Array

Range from lowest to highest

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 3824 Views - Last Post: 18 February 2010 - 07:21 PM Rate Topic: -----

#16 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 04:37 PM

Kind of need some guidance with the if statments in the quiz range.
#include <stdio.h>
#include <stdlib.h>
#define STUDENTS 5 // row
#define QUIZZES 7 // column

main(){

	int qScores[STUDENTS][QUIZZES] = {
		{ 88, 77, 54, 65, 77, 98, 65 },
		{ 56, 86, 97, 50, 75, 100, 74 },
		{ 60, 89, 58, 86, 90, 60, 79 },
		{ 44, 76, 90, 95, 95, 95, 97 },
		{ 65, 86, 88, 89, 51, 92, 75 }
	};
	int studentTotal = 0, quizTotal = 0, row, col;
	int  topStudent = -1, low = -1, high = 100; // <- intialized the high and low
	double studentAverage, highestAverage = 0,lowestAverage = 0, quizAverage;

	for( row = 0; row < STUDENTS; row++ ){
		studentTotal = 0;
		for( col = 0; col < QUIZZES; col++ ){
			studentTotal += qScores[row][col];
		}
		studentAverage = (double) studentTotal / QUIZZES;
		printf("Student %i has an average of %.2lf\n\n", row, studentAverage);
		if( studentAverage > highestAverage){
			highestAverage = studentAverage;
			topStudent = row;
		}
	}

	for( col = 0; col < QUIZZES; col++ ){
		quizTotal = 0;
		for( row = 0; row < STUDENTS; row++ ){
			quizTotal += qScores[row][col];
		}
		quizAverage = (double) quizTotal / STUDENTS;

		printf("Quiz %i has an average of %.2lf\n\n", col, quizAverage);
	}
	printf("Student %i has the highest average with a score of %.2lf\n\n\n", topStudent, highestAverage);

	printf("Quiz Grades Range\n");
	printf("-----------------\n");


	for( col = 0; col < QUIZZES; col++ ){
		if(/* Need condition here i think*/){
		}
		for( row = 0; row < STUDENTS; row++ ){
			if(/*Need condition here*/){
			}
			// Do both if statements go here? Where do I put the high  and low?
		}
	}

	printf("Quiz %i: %i - %i\n", col, low, high); 


	

	system("pause");

}

This post has been edited by sf18: 17 February 2010 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#17 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array

Posted 17 February 2010 - 04:50 PM

No if() goes between the for()s because the idea is to scan the student grades for each test. What does go there is the initial values for the variables high and low. Because they need to be reset for each quiz.

That help?
Was This Post Helpful? 0
  • +
  • -

#18 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 04:56 PM

@n8wxs O okay I think I get what you're saying. Let me try to implement it.
Was This Post Helpful? 0
  • +
  • -

#19 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 05:08 PM

Okay I feel I am getting a bit closer but some problems have arised. Take a look:

	for( col = 0; col < QUIZZES; col++ ){
		if(col > low || col < high){
			}
			for( row = 0; row < STUDENTS; row++ ){
			if(row > low || row < high){
			}
		}
		// Is this correct?
		high = 0;
		low = 100;

		printf("Quiz %i: %i - %i\n", col, low, high); 
	}


	

	system("pause");

}


Here is the output:

Quote

Student 0 has an average of 74.86

Student 1 has an average of 76.86

Student 2 has an average of 74.57

Student 3 has an average of 84.57

Student 4 has an average of 78.00

Quiz 0 has an average of 62.60

Quiz 1 has an average of 82.80

Quiz 2 has an average of 77.40

Quiz 3 has an average of 77.00

Quiz 4 has an average of 77.60

Quiz 5 has an average of 89.00

Quiz 6 has an average of 78.00

Student 3 has the highest average with a score of 84.57


Quiz Grades Range
-----------------
Quiz 0: 100 - 0
Quiz 1: 100 - 0
Quiz 2: 100 - 0
Quiz 3: 100 - 0
Quiz 4: 100 - 0
Quiz 5: 100 - 0
Quiz 6: 100 - 0
Press any key to continue . . .


It seems to be going high to low first of all and every quiz range is the same. It is taking from high and low? What did I do wrong?

This post has been edited by sf18: 17 February 2010 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#20 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Array

Posted 17 February 2010 - 05:13 PM

Think about this:
if(col > low || col < high){
                        }

What exactly are you expecting this code to do?
Was This Post Helpful? 0
  • +
  • -

#21 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 05:23 PM

View PostJackOfAllTrades, on 17 February 2010 - 04:13 PM, said:

What exactly are you expecting this code to do?


Not sure if this was a rhetorical question but I want a range from low to high for each quiz. But I guess it is obvious that my if-statments are incorrect. I'm trying to do some different solutions but it is just not working out for me.
Was This Post Helpful? 0
  • +
  • -

#22 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Array

Posted 17 February 2010 - 05:36 PM

There is nothing inside your if statements.
Was This Post Helpful? 0
  • +
  • -

#23 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 05:40 PM

Ok so instead of using 'low' and 'high'. . . I should put something else in the if statments? But I'm not sure what to put then? QUIZZES, STUDENTS??? Make up another intializer? I dont know
Was This Post Helpful? 0
  • +
  • -

#24 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 05:46 PM

Yea just commented out my if statments and get the same output. So what condition do I use in the if-statments?
Was This Post Helpful? 0
  • +
  • -

#25 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 06:47 PM

Still confused . . .I'm trying some different stuff but keep getting error messages and trying to reference my book but nothing in there about range. I really want to get this :(

It's my if-statment...

??
Was This Post Helpful? 0
  • +
  • -

#26 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array

Posted 17 February 2010 - 07:11 PM

Earlier in your code you print out the student averages and the quiz averages. Use the same framework to print out the grade ranges. Instead of computing an average, you need to keep track of both high and low grades.
Was This Post Helpful? 0
  • +
  • -

#27 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 17 February 2010 - 07:14 PM

@n8wxs
Yea I know that is what I am having some trouble with...

This post has been edited by sf18: 17 February 2010 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#28 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 18 February 2010 - 06:20 AM

...

This post has been edited by sf18: 18 February 2010 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#29 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Array

Posted 18 February 2010 - 05:47 PM

When I thought I finally figured it out, it goes all wrong...
main(){

	int qScores[STUDENTS][QUIZZES] = {
		{ 88, 77, 54, 65, 77, 98, 65 },
		{ 56, 86, 97, 50, 75, 100, 74 },
		{ 60, 89, 58, 86, 90, 60, 79 },
		{ 44, 76, 90, 95, 95, 95, 97 },
		{ 65, 86, 88, 89, 51, 92, 75 }
	};
	int studentTotal = 0, quizTotal = 0, row, col;
	int  topStudent = -1, low = 100, high = 0; 
	double studentAverage, highestAverage = 0,lowestAverage = 0, quizAverage;

	for( row = 0; row < STUDENTS; row++ ){
		studentTotal = 0;
		for( col = 0; col < QUIZZES; col++ ){
			studentTotal += qScores[row][col];
		}
		studentAverage = (double) studentTotal / QUIZZES;
		printf("Student %i has an average of %.2lf\n", row, studentAverage);
		if( studentAverage > highestAverage){
			highestAverage = studentAverage;
			topStudent = row;
		}
	}
	printf("\n\n");

	for( col = 0; col < QUIZZES; col++ ){
		quizTotal = 0;
		for( row = 0; row < STUDENTS; row++ ){
			quizTotal += qScores[row][col];
		}
		quizAverage = (double) quizTotal / STUDENTS;

		printf("Quiz %i has an average of %.2lf\n", col, quizAverage);
	}

	printf("\n\n");

	printf("Student %i has the highest average with a score of %.2lf\n\n\n", topStudent, highestAverage);

	printf("Range\n");
	

        // Here is the range code
	for( col = 0; col < QUIZZES; col++ ){
			for( row = 0; row < STUDENTS; row++ ){
				if(qScores[row][col] < low){
					low = qScores[row][col];
				}
				if(qScores[row][col] > high){
					high = qScores[row][col];
				}
				
				//high = 0;
				//low = 100;
			}

		printf("Quiz %i: %i - %i\n", col, low, high); 
	}

} 

Here is my Output:
Student 0 has an average of 74.86
Student 1 has an average of 76.86
Student 2 has an average of 74.57
Student 3 has an average of 84.57
Student 4 has an average of 78.00


Quiz 0 has an average of 62.60
Quiz 1 has an average of 82.80
Quiz 2 has an average of 77.40
Quiz 3 has an average of 77.00
Quiz 4 has an average of 77.60
Quiz 5 has an average of 89.00
Quiz 6 has an average of 78.00


Student 3 has the highest average with a score of 84.57


Range
Quiz 0: 44 - 88 <- It seemed to be working for the fist column
Quiz 1: 44 - 89 < - But then 'low' values are all the same
Quiz 2: 44 - 97
Quiz 3: 44 - 97 < And then the 'high' values are not right either
Quiz 4: 44 - 97
Quiz 5: 44 - 100
Quiz 6: 44 - 100
Press any key to continue . . .


Any suggestions on why its not working properly?

This post has been edited by sf18: 18 February 2010 - 05:51 PM

Was This Post Helpful? 0
  • +
  • -

#30 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array

Posted 18 February 2010 - 05:55 PM

You need to reinitialize high and low for each quiz.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3