NO

Author Topic: sum of array  (Read 6906 times)

sannemander

  • Guest
sum of array
« 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:
Code: [Select]
#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);
   

}

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: sum of array
« Reply #1 on: November 28, 2011, 07:29:05 PM »
Think about the for() statement for more than a second, or two...  ;)

Ralf

sannemander

  • Guest
Re: sum of array
« Reply #2 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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: sum of array
« Reply #3 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  ;)

sannemander

  • Guest
Re: sum of array
« Reply #4 on: November 29, 2011, 07:38:44 PM »
ok think i got it, like this:

Code: [Select]
#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!!

CommonTater

  • Guest
Re: sum of array
« Reply #5 on: November 29, 2011, 07:47:23 PM »
Try it like this...
Code: [Select]
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!)

sannemander

  • Guest
Re: sum of array
« Reply #6 on: November 29, 2011, 08:02:53 PM »
like this:

Code: [Select]
   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);
   

}

CommonTater

  • Guest
Re: sum of array
« Reply #7 on: November 29, 2011, 10:45:17 PM »
like this:

Code: [Select]
   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...
Code: [Select]
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.




sannemander

  • Guest
Re: sum of array
« Reply #8 on: November 30, 2011, 02:41:37 PM »
Ah of course not, make the program slower no?

thx for helping!

CommonTater

  • Guest
Re: sum of array
« Reply #9 on: November 30, 2011, 03:34:35 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?"





Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: sum of array
« Reply #10 on: December 02, 2011, 01:02:10 AM »
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 been
Code: [Select]
for (y = 0, x > y, y++)That was all that was wrong and what needed to be changed...

Ralf

sannemander

  • Guest
Re: sum of array
« Reply #11 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

CommonTater

  • Guest
Re: sum of array
« Reply #12 on: December 02, 2011, 07:51:35 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.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: sum of array
« Reply #13 on: December 03, 2011, 07:31:43 AM »
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

CommonTater

  • Guest
Re: sum of array
« Reply #14 on: December 03, 2011, 07:49:21 AM »
:D  As they say ... C is easy ... Programming is hard.