Pelles C forum

C language => Beginner questions => Topic started by: sannemander on November 28, 2011, 06:30:19 PM

Title: sum of array
Post by: sannemander on November 28, 2011, 06:30:19 PM
Hello!

I got a new question , i need to find the sum of the array (only the numbers inputted) and really can“t figure it out.
This is the code:
#include <stdio.h>
#include <conio.h>




int main (void)
{
int x=0;
int y=0;
float iArray [30];
char cResponse = '\0';
    float sum=0;
float Ave=0;



while (x>0 || x < 30)
{       
          _clrscr();
printf ("Enter GPA: ");
    scanf ("%f", &iArray[x++]);
    printf("enter another GPA(Y/N?): ");
        scanf(" %c", &cResponse);                 

if     ((cResponse == 'n') || (cResponse ==  'N')){

break;}}
     


for (y=0;x < y; y++)
    sum += iArray[x];


Ave = sum / y;

printf("\nthe average GPA = %f\n", Ave);
   

}
Title: Re: sum of array
Post by: Bitbeisser on November 28, 2011, 07:29:05 PM
Think about the for() statement for more than a second, or two...  ;)

Ralf
Title: Re: sum of array
Post by: sannemander on November 28, 2011, 09:21:48 PM
ok gonna try my best, see that the for loop is a mistake, been staring to long at the pc so need a coffee break i quess! ;D
Title: Re: sum of array
Post by: Bitbeisser on November 29, 2011, 03:02:52 AM
Ok, I am gonna throw you some breadcrumbs here...  ;)

The for() loop is not a mistake, it is just not declared properly.

You have three parts in the loop statement:
- initialization (y = 0)
- terminal condition (x < y)
- increment (y++)

Now initialization and increment are fairly obvious and correct, but think about that terminal condition...  8)

Ralf  ;)
Title: Re: sum of array
Post by: sannemander on November 29, 2011, 07:38:44 PM
ok think i got it, like this:

#include <stdio.h>
#include <conio.h>




int main (void)
{
int x=0;
int y;
float iArray [30];
char cResponse = '\0';
    float Ave=0;
float sum=0;



while (x>0 || x < 30)
{       
          _clrscr();
printf ("Enter GPA: ");
    scanf ("%f", &iArray[x++]);
    printf("enter another GPA(Y/N?): ");
        scanf(" %c", &cResponse);                 

if     ((cResponse == 'n') || (cResponse ==  'N')){

break;}}
   

_clrscr();


   for(y=0;x>-1;y++){
   sum += iArray[x--];
if(x>-1){
   printf("\nGPA:%f\n", iArray[x]);}
   
   Ave = sum / y;
}


printf("\nthe average GPA = %f\n", Ave);
   

}



THANKS!!
Title: Re: sum of array
Post by: CommonTater on November 29, 2011, 07:47:23 PM
Try it like this...

sum = 0;
for (y = 0; y < x; y++)
  sum += iArray[y];

Use the same basic loop concept to print the array... (and almost everywhere else, too!)
Title: Re: sum of array
Post by: sannemander on November 29, 2011, 08:02:53 PM
like this:

   for(y=0;y<x;y++){
   sum += iArray[y];
   Ave = sum / x;
   printf("\nGPA:%f\n", iArray[y]);}
   

printf("\nthe average GPA = %f\n", Ave);
   

}
Title: Re: sum of array
Post by: CommonTater on November 29, 2011, 10:45:17 PM
Quote from: sannemander on November 29, 2011, 08:02:53 PM
like this:

   for(y=0;y<x;y++){
   sum += iArray[y];
   Ave = sum / x;
   printf("\nGPA:%f\n", iArray[y]);}
   
   
   printf("\nthe average GPA = %f\n", Ave);
   
   
}



More like this...

for(y=0;y<x;y++)
  {
   sum += iArray[y];
   printf("\nGPA : %f\n", iArray[y]);
  }

Ave = sum / x;
printf("\nthe average GPA = %f\n", Ave);


There's no reason to recalculate the average every time through the  loop.



Title: Re: sum of array
Post by: sannemander on November 30, 2011, 02:41:37 PM
Ah of course not, make the program slower no?

thx for helping!
Title: Re: sum of array
Post by: CommonTater on November 30, 2011, 03:34:35 PM
Quote from: sannemander on November 30, 2011, 02:41:37 PM
Ah of course not, make the program slower no?

thx for helping!

Well, on a small practice piece like this it doesn't make enough difference to sniff at but in larger projects where you are doing a lot of repetative stuff it can (and does) make a very noticeable difference. 

But there is considerable merrit in forming good habits early. :D

When I program I work in several phases...
1) Thinking about what I need to do...
2) Writing up a step by step plan...
3) Writing the code, most often just getting it to work *at all*.
4) Testing and improving the code to get the biggest bang for the buck from it.
5) Hoping I never have to do that again... LOL.

There are a couple of ground level "rules" you can adopt to help you along...
1) "Compiles" does not mean "Works".
2) Comments are our friends.
3) Never create a variable you don't absolutely need.
4) Never be afraid to look stuff up  (i.e. wear out your F1 key!)
5) Treat all compiler messages as issues to be fixed.
6) When being your most clever, ask: "If I come back to this in 5 years, will I still be able to follow it?"




Title: Re: sum of array
Post by: Bitbeisser on December 02, 2011, 01:02:10 AM
Quote from: sannemander on November 30, 2011, 02:41:37 PM
Ah of course not, make the program slower no?

thx for helping!
All you had to do was to change the "x < y" part of the for() statement!

It was a very simple logic error. When you start with y=0 and x has after the data entry loop a value >0, that expression (x < y) simply could never be true, therefor the loop was never executed.
Correctly, it would should have beenfor (y = 0, x > y, y++)That was all that was wrong and what needed to be changed...

Ralf
Title: Re: sum of array
Post by: sannemander on December 02, 2011, 12:18:32 PM
THX to both for the info, really helps me(and the pc from not being thrown out of the window)! ;D
Title: Re: sum of array
Post by: CommonTater on December 02, 2011, 07:51:35 PM
Quote from: sannemander on December 02, 2011, 12:18:32 PM
THX to both for the info, really helps me(and the pc from not being thrown out of the window)! ;D

Believe me, I've been in that position more than once! 

Glad to help out.
Title: Re: sum of array
Post by: Bitbeisser on December 03, 2011, 07:31:43 AM
Quote from: sannemander on December 02, 2011, 12:18:32 PM
THX to both for the info, really helps me(and the pc from not being thrown out of the window)! ;D
No problem, and I know that feeling. Take a guess why my kitchen window at home is always open...  ;D

Don't know if you are starting out with programming by programming in C, but it is my  firm believe that C is not a good beginners programming language, specially if you are doing it on your own, without any direct guidance.

Programming has to do a lot with very basic logic, which is independent from the programming language used. Language specific implementation are rather an additional hurdle, which is easier to cross when some of the very basic concepts of programming are understood. And if you have those basics down, learning a/any programming language is sooo much easier...

Ralf
Title: Re: sum of array
Post by: CommonTater on December 03, 2011, 07:49:21 AM
:D  As they say ... C is easy ... Programming is hard.

Title: Re: sum of array
Post by: Bitbeisser on December 04, 2011, 06:08:27 AM
Quote from: CommonTater on December 03, 2011, 07:49:21 AM
:D  As they say ... C is easy ... Programming is hard.
Are you sure you didn't get that backwards?  ;D

Ralf  8)