Little array issue
Author |
Message |
Ninja
|
Posted: Thu Mar 13, 2008 5:56 pm Post subject: Little array issue |
|
|
Hey guys!.Ok so i was working on this assignment.Basically here is what it is.An array holds marks entered by the user.We have to assume that the user wont enter more than 40 values. So i wrote the code for it (sorry for any sloppyness or errors, still working on it lol).The user can terminate the program by entering -1..but when you go to print the array (say you enter 4 marks) it prints all 40 instead of just the 4 entered.Here is the code...can anyone tell me what i'm doing wrong here cause i can't figure out.Thanks in advance!
code: |
#include <stdio.h>
#include <stdlib.h>
#define MAX 40
void printArray(float marks[],int arr_size);
float calcMedian(float marks[ ], int arr_size);
void sortArray(float marks[], int array_size);
isValid(float score);
enum bool {true,false};
float marks_valid[MAX];
main ()
{
int i,choice,valid = 0,invalid = 0;
float marks,sum = 0,mean = 0,median,min = 0,max = 0;
for (i=0; i < MAX; i++)
{
printf("Enter mark:");
scanf("%e",&marks);
fflush(stdin);
if (marks == -1)
{
break;
}
else if (isValid(marks)== true)
{
valid++;
marks_valid[i] = marks;
min = (marks_valid[i] < min)? marks_valid[i]: min;
max = (marks_valid[i] > max)? marks_valid[i]: max;
sum+= marks_valid[i];
}
else
{
invalid++;
}
}
system("cls");
mean = sum/valid;
printf("Valid: %d\n",valid);
printf("Invalid: %d\n",invalid);
getch();
system("cls");
sortArray(marks_valid, MAX);
printf("**************MAIN MENU**************\n");
printf("1.Print test scores\n");
printf("2.Print stats\n");
printf("**************MAIN MENU**************\n");
scanf("%d",&choice);
if(choice == 1)
{
system("cls");
printArray(marks_valid,MAX);
getch();
}
else if (choice == 2)
{
printf("Sum: %.2lf\n",sum);
printf("Average: %.2lf\n",mean);
printf("Min score: %.2lf\n",min);
printf("Max score: %.2lf\n",max);
getch();
}
}
void printArray(float marks[],int marks_size)
{
int i;
sortArray(marks_valid, MAX);
for (i = 0; i < MAX; i++)
{
printf("Test Scores entered: %.2lf \n",marks[i]);
}
}
isValid(float score)
{
if (score >= 0 && score <= 100)
{
printf("Valid\n");
return true;
}
else
{
printf("Invalid!\n");
return false;
}
}
void sortArray(float marks[], int array_size)
{
int i, j;
float index;
for (i=1; i < array_size; i++)
{
index = marks[i];
j = i;
while ((j > 0) && (marks[j-1] > index))
{
marks[j] = marks[j-1];
j = j - 1;
}
marks[j] = index;
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: Thu Mar 13, 2008 6:00 pm Post subject: RE:Little array issue |
|
|
code: | for (i = 0; i < MAX; i++)
{
printf("test Scores entered: %.2lf \n",marks[i]);
} | instead of from 1 to 40, you can add a counter for everytime they enetered a number, and go from there
code: | for (int i = 0 ; i < counter; i++)
{
printf("test Scores entered: %.2lf \n",marks[i]);
} |
|
|
|
|
|
|
Saad
|
Posted: Thu Mar 13, 2008 6:01 pm Post subject: Re: Little array issue |
|
|
code: | void printArray(float marks[],int marks_size)
{
int i;
sortArray(marks_valid, MAX);
for (i = 0; i < MAX; i++)
{
printf("test Scores entered: %.2lf \n",marks[i]);
}
}
|
You use MAX as your number of scores entered when printing them out
Hoever you should be marks_size that you pass to the printArray function.
Saad |
|
|
|
|
|
Ninja
|
Posted: Thu Mar 13, 2008 7:02 pm Post subject: Re: RE:Little array issue |
|
|
HeavenAgain @ Thu Mar 13, 2008 6:00 pm wrote: code: | for (i = 0; i < MAX; i++)
{
printf(" Scores entered: %.2lf \n",marks[i]);
} | instead of from 1 to 40, you can add a counter for everytime they enetered a number, and go from there
code: | for (int i = 0 ; i < counter; i++)
{
printf(" Scores entered: %.2lf \n",marks[i]);
} |
Hmmm...but the array is still initialized with a size of 40..any way to change the size of the array by counting how many valid numbers the user enters.Ahhh..i wish i could use C++ for this class, vectors make life easier lol. |
|
|
|
|
|
HeavenAgain
|
Posted: Thu Mar 13, 2008 7:12 pm Post subject: RE:Little array issue |
|
|
make your valid variable global, and then you'll have
code: | for (int i = 0 ; i < valid; i++)
{
printf(" Scores entered: %.2lf \n",marks[i]);
} | you dont change the size or anything, just keeping track of how many valid are entered, and use that number to print from 0 - numberValid-1 (0 base index), get what i mean?
eg. your array size is 40, currently is empty with 0s
now i entered 1, 2, 3, -1, 4, 5
i entered 6 numbers, valid now is 5, and i only stored 1, 2, 3, 4, 5 (with respect to their index)
and now i want to print that number, so we use valid, which is 5, and print.
edit: as Saad pointed out, your parameter, you passed in "printArray(marks_valid,MAX); " MAX, but instead maybe you should pass in valid, and then you dont have to make valid a global variable. |
|
|
|
|
|
Ninja
|
Posted: Thu Mar 13, 2008 7:37 pm Post subject: Re: Little array issue |
|
|
OMG haha..so get this.I did what saad said, but i typed it wrong and it gave me an error..so out of frustration i was like to hell with this! lol. But i got it working now, actually i just thought of what u said and it works , and then i read your reply haha.Thanks a lot guys!!
Now i can go email this to my class cause they have no clue what to do lol. |
|
|
|
|
|
A.J
|
Posted: Thu Mar 13, 2008 7:39 pm Post subject: Re: Little array issue |
|
|
As HeavenAgain said, you could use a counter |
|
|
|
|
|
|
|