Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 19, 2011, 08:15 PM   #1
CsRookie
macrumors newbie
 
Join Date: Apr 2011
Variable Length Array in C

Code:
#define SIZE 10
#define STUDENT_NUM 10
#define EXAM_GRADE 3

int main()

{
	int ids [SIZE],i, id;
	double scores [SIZE];
	int total = 0.0;
	int GPA;
	
	for (i=0; i < SIZE; i++)
	{
	
		printf("Enter student ID number (negative to stop)\n");
		scanf("%d", &ids[i]);
		printf ("Enter exam score 1 for student %d now\n", ids[i]);
		scanf("%d", &scores[i]);
		printf ("Enter exam score 2 for student %d now\n",ids[i]);
		scanf("%d", &scores[i]);
		printf ("Enter exam score 3 for student %d now\n",ids[i]);
		scanf("%d", &scores[i]);
	
	}
What type of statement, or loop do I need to add in order to be able to cut this off when I want to (with a negative student id). I tried adding a nested while loop but it is not working but part of the issue is im not sure if im looking for the right thing. I tried while ids > 0. Can someone point me in the right direction? Thanks in advance

Last edited by CsRookie; Apr 20, 2011 at 12:03 AM.
CsRookie is offline   0 Reply With Quote
Old Apr 19, 2011, 08:20 PM   #2
KnightWRX
macrumors Pentium
 
KnightWRX's Avatar
 
Join Date: Jan 2009
Location: Quebec, Canada
Just change your loop's exit condition to check that the id entered is positive ? You'll have to rework some of your output and input code there to check the id at the proper time. What other looping statements have you learned that you think would be a better fit ?

The other problem you'll be facing is that your array has a static size of 10. After 10 ids, if you still don't encounter a negative id, you'll have a buffer overflow.

Oh and you're overwriting your scores. You only ever keep exam score 3.
__________________
"What you leave behind is not what is engraved in stone monuments, but what is woven into the lives of others."
-- Pericles
KnightWRX is offline   0 Reply With Quote
Old Apr 19, 2011, 08:32 PM   #3
CsRookie
Thread Starter
macrumors newbie
 
Join Date: Apr 2011
Quote:
Originally Posted by KnightWRX View Post
Just change your loop's exit condition to check that the id entered is positive ? You'll have to rework some of your output and input code there to check the id at the proper time. What other looping statements have you learned that you think would be a better fit ?

The other problem you'll be facing is that your array has a static size of 10. After 10 ids, if you still don't encounter a negative id, you'll have a buffer overflow.

Oh and you're overwriting your scores. You only ever keep exam score 3.
You are right, how do I avoid overflow? Do I need to declare it differently? Is there a way to declare a variable length array that still fills up at 10 or displays a message after 10 not allowing anymore?
I have a good understanding of the majority of the loops (while,dowhile, for, etc.) I am more confused on arrays. I would think the easiest way would be to say

Code:
do
{

}while(ids > 0);
but I do not think ids is the condition I should be testing.

Last edited by CsRookie; Apr 19, 2011 at 08:47 PM.
CsRookie is offline   0 Reply With Quote
Old Apr 19, 2011, 09:02 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Look up the break keyword for a looping context. break also has a use in switch context, which isn't relevant here.

Typical use of break in a looping context:
Code:
if ( some terminating condition )
  break;

Quote:
Originally Posted by KnightWRX View Post
The other problem you'll be facing is that your array has a static size of 10. After 10 ids, if you still don't encounter a negative id, you'll have a buffer overflow.
I don't see it: the loop is bounded by i < SIZE, where SIZE is the length of both arrays.

That doesn't mean there aren't significant bugs in the posted code. Just that I don't see where either array will overflow, as given.

A whole lot more thought needs to go into the design. Such as the fact that up to 30 scores might be stored (3 per student, max 10 students), but the array for storing scores is plainly insufficient for this task.

Last edited by chown33; Apr 19, 2011 at 09:20 PM.
chown33 is offline   0 Reply With Quote
Old Apr 19, 2011, 09:13 PM   #5
Bill McEnaney
macrumors 6502
 
Join Date: Apr 2010
[QUOTE=CsRookie;12424376]
Code:
#define SIZE 10
#define STUDENT_NUM 10
#define EXAM_GRADE 3

int main()

{
	int ids [SIZE],i, id;
	double scores [SIZE];
	int total = 0.0;
	int GPA;
	
	for (i=0; i < SIZE; i++)
	{
	
		printf("Enter student ID number (negative to stop)\n");
		scanf("%d", &ids[i]);
		printf ("Enter exam score 1 for student %d now\n", ids[i]);
		scanf("%d", &scores[i]);
		printf ("Enter exam score 2 for student %d now\n",ids[i]);
		scanf("%d", &scores[i]);
		printf ("Enter exam score 3 for student %d now\n",ids[i]);
		scanf("%d", &scores[i]);
	
	}
Correct me if I'm wrong. But I think the blue scanf overwrites what the red one put into scores[i] and that the green scanf what the blue one put there.
Bill McEnaney is offline   0 Reply With Quote
Old Apr 19, 2011, 09:30 PM   #6
CsRookie
Thread Starter
macrumors newbie
 
Join Date: Apr 2011
Quote:
Originally Posted by chown33 View Post
Look up the break keyword for a looping context. break also has a use in switch context, which isn't relevant here.

Typical use of break in a looping context:
Code:
if ( some terminating condition )
  break;



I don't see it: the loop is bounded by i < SIZE, where SIZE is the length of both arrays.

That doesn't mean there aren't significant bugs in the posted code. Just that I don't see where either array will overflow, as given.

A whole lot more thought needs to go into the design. Such as the fact that up to 30 scores might be stored (3 per student, max 10 students), but the array for storing scores is plainly insufficient for this task.

Thank you very much!

I added more arrays now such as score1,score2,score3 to help with the fact that 30 scores needed to be displayed and the break statement works great for exiting. Thank you again.

Another question I have is, how do I make it so only the arrays which have information pertaining to the program display at the end?

For example if I stop the program after 3 students and scores the display at the end with gpa and averages etc still shows all 10 arrays slots and has huge strings of numbers displaying. How do I show only those with data?
CsRookie is offline   0 Reply With Quote
Old Apr 19, 2011, 09:37 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by CsRookie View Post
How do I show only those with data?
Keep the array index when you break the loop.
chown33 is offline   0 Reply With Quote
Old Apr 19, 2011, 09:53 PM   #8
CsRookie
Thread Starter
macrumors newbie
 
Join Date: Apr 2011
Quote:
Originally Posted by chown33 View Post
Keep the array index when you break the loop.
got it working now.

Thank you again for the help.
CsRookie is offline   0 Reply With Quote
Old Apr 20, 2011, 12:06 AM   #9
CsRookie
Thread Starter
macrumors newbie
 
Join Date: Apr 2011
Code:
#include <stdio.h>

#define SIZE 10
#define STUDENT_ID 10
#define EXAM_SCORE 3

int main()

{
	int ids [SIZE],i;
	double scores1 [SIZE];
	double scores2 [SIZE];
	double scores3[SIZE];
	int total = 0.0;
	double sum1=0;
	double sum2=0;
	double sum3=0;
	double studentAvg;
	double a =0, b = 0, c=0,d=0,f=0;
	int count=0.00;
	
	for (i=0; i < SIZE; i++)

	{
	
		printf("Enter student ID number (negative to stop)\n");
		scanf("%d", &ids[i]);
		if (ids[i] < 0)
		break;
		printf ("Enter exam score 1 for student %d now\n", ids[i]);
		scanf("%lf", &scores1[i]);
		sum1+=scores1[i];
		printf("Enter exam score 2 for student %d now\n",ids[i]);
		scanf("%lf", &scores2[i]);
		sum2+=scores2[i];
		printf("Enter exam score 3 for student %d now\n",ids[i]);
		scanf("%lf", &scores3[i]);
		sum3+=scores3[i];
		++count;
		
	}
	printf("\n----------------\n");
	printf("Summary\n");
	printf("-----------------\n");

	printf("\n-------------------------------------------------------------------\n");
	printf("ID #	Exam 1		Exam 2		Exam 3		Average	Grade   ");
	printf("\n-------------------------------------------------------------------\n");
	
	

	
	for (i = 0; i < SIZE; i++)
	{
		if (ids[i] < 0)
			break;
		printf("\nID %d \t", ids[i]);
		printf("%.2f		", scores1[i]);
		printf("%.2f		", scores2[i]);
		printf("%.2f          ", scores3[i]);
		printf("%.2f   ",(scores1[i]+scores2[i]+scores3[i])/3); 
		if ((scores1[i]+scores2[i]+scores3[i])/3 > 89.5)
			printf("A\n");
		else if ((scores1[i]+scores2[i]+scores3[i])/3 > 79.5)
			printf("B\n");
		else if ((scores1[i]+scores2[i]+scores3[i])/3 > 69.5)
			printf("C\n");
		else if ((scores1[i]+scores2[i]+scores3[i])/3 > 59.5)
			printf("D\n");
		else if ((scores1[i]+scores2[i]+scores3[i])/3 < 59.4)
			printf("F\n");


		if ((scores1[i]+scores2[i]+scores3[i])/3 > 89.5)
			++a;
		else if ((scores1[i]+scores2[i]+scores3[i])/3 > 79.5)
			++b;
		else if ((scores1[i]+scores2[i]+scores3[i])/3 > 69.5)
			++c;
		else if ((scores1[i]+scores2[i]+scores3[i])/3 > 59.5)
			++d;
		else if ((scores1[i]+scores2[i]+scores3[i])/3 < 59.4)
			++f;
	}
	
	printf("\n\nSummary Report\n");
	printf("\n-----------------\n");
	printf("\n");
	
	
	printf("Exam 1 average     %.2f", sum1/count);
	printf("\n");
	printf("Exam 2 average     %.2f", sum2/count);
	printf("\n");
	printf("Exam 3 average     %.2f", sum3/count);
	printf("\n");
	printf("Overall average    %.2f", (sum1+sum2+sum3)/count);
	printf("\n");
	
	printf("Class GPA = %.2f", (a*4+b*3+c*2+d*1+f*0)/count);
	
	printf("\n");
	




	
return 0;
}


Can anyone see why my overall average is off? it seems to only add the three sums together and completely ignores dividing by the count.

Also I am having issues trying to sort the data, I would like to sort by highest average, what direction do I need to look into for that. I am trying to work with bubble sorting but I am struggling to apply the concept in relation to this program.

Last edited by CsRookie; Apr 20, 2011 at 02:03 AM.
CsRookie is offline   0 Reply With Quote
Old Apr 20, 2011, 01:18 AM   #10
ender land
macrumors 6502a
 
Join Date: Oct 2010
you can get weird results for a variety of reasons when doing something that amounts to

double / integer

printf sometimes does REALLY weird display things when you put integers as floats, if you are using xcode try using the debugger and putting a breakpoint there or create a temporary variable to see.

Otherwise, you might want to try
Quote:
(sum1+sum2+sum3)/(double)count);
to ensure you are actually getting a %f value to output.

you can also do
Code:
	printf("Exam 1 average     %.2f \n", sum1/count);
and save yourself a line of code btw

Why are you using references to those arrays? Why not just use the absolute reference? That seems a bit more complicated than would be needed. You might also be able to clean up that if/elseif stuff if you look up Switch statements, they more or less are made to do what you are doing
ender land is offline   0 Reply With Quote
Old Apr 20, 2011, 04:02 AM   #11
Bill McEnaney
macrumors 6502
 
Join Date: Apr 2010
Why not something like this?

Code:
average = (scores1[i] + scores2[i] + scores3[i]) / 3.0;
if (average > 89.5)
  a++;
else if (average > 79.5)
  b++;
else if (average > 69.5)
   c++;
else if (average > 59.5)
  d++;
else
  f++;
I don't see any way to use a switch statement in our friend's program because I doubt that C would let us write anything like this.
Code:
switch (average)
{
  case > 89.5:
    a++;
    break;

  case > 79.5:
    b++;
    break;

  case > 69.5:
    c++;
    break;

  case > 59.5:
    d++;
    break;

  default:
    f++;
}

Last edited by Bill McEnaney; Apr 20, 2011 at 08:25 AM.
Bill McEnaney is offline   0 Reply With Quote
Old Apr 20, 2011, 07:08 AM   #12
dmi
macrumors member
 
Join Date: Dec 2010
Quote:
Originally Posted by Bill McEnaney View Post
Code:
switch (average)
{
  case > 89.5:
    a++;
    break;

  case > 79.5:
    b++;
    break;

  case > 69.5:
    c++;
    break;

  case > 59.5:
    d++;
    break;

  default:
    f++;
}
Code:
switch ((int)((average+.5)/10))
{

  case 8:
    b++;
    break;

  case 7:
    c++;
    break;

  case 6:
    d++;
    break;

  case 0:  case 1:  case 2:  case 3:  case 4:  case 5:
    f++;
    break;

  default:
    a++;
}
dmi is offline   0 Reply With Quote
Old Apr 20, 2011, 08:32 AM   #13
Bill McEnaney
macrumors 6502
 
Join Date: Apr 2010
Dmi, that was clever.
Bill McEnaney is offline   1 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
R-statistical: Changing categorical variables from text to numbers Erniecranks Mac Programming 6 May 8, 2011 07:30 PM
reading variable length lines from a file farmerdoug Mac Programming 2 Feb 26, 2011 01:07 PM
Variables in Arrays in Objectives-C eatpie26 iPhone/iPad Programming 27 Jan 5, 2011 08:21 PM
working with arrays in a hurry in c farmerdoug Mac Programming 43 Mar 5, 2010 10:15 PM
Array in array nicoko iPhone/iPad Programming 5 Jan 12, 2009 11:40 AM


All times are GMT -5. The time now is 01:02 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC