954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

finding the sum of an array

the basic idea of what im trying to do is write a program that reads an arbitrary number of integers less than 20. then it prints out the position of the integer in the order the user inputed it then it finds the sum when it scans a non integer character like *. so it would look something like this if it run correctly

36 27 55 -3 42 66 -5 *

Entered integer number 0: was 36
Entered integer number 1: was 27
Entered integer number 2: was 55
Entered integer number 3: was -3
Entered integer number 4: was 42
Entered integer number 5: was 66
Entered integer number 6: was -5
The sum is: 218

i have written this so far

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    i++;
  }

  int n = SIZE;
  for (i = 0; i < n;){
    sum = sum +  array[i];
    printf( "The sum is: %d\n", sum);      
  }
  return 0;
}


it pretty much does the first part of reading out the position of the integers but it goes nuts when its time to find the sum. any help would be appreciated

hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

You forgot the i++ in the first line of the for loop.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Define "goes nuts". That is hard to imagine.

Explaining a problem correctly often results in more accurate answers and better help.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>>for (i = 0; i < n;++i){

What will happen if you only input 3 numbers? Answer: the final value of sum will be in the crapper because it will add in uninitialized array elements 4-20.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

the basic idea of what im trying to do is write a program that reads an arbitrary number of integers less than 20. then it prints out the position of the integer in the order the user inputed it then it finds the sum when it scans a non integer character like *. so it would look something like this if it run correctly

36 27 55 -3 42 66 -5 *

Entered integer number 0: was 36 Entered integer number 1: was 27 Entered integer number 2: was 55 Entered integer number 3: was -3 Entered integer number 4: was 42 Entered integer number 5: was 66 Entered integer number 6: was -5 The sum is: 218

i have written this so far

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    i++;
  }

  int n = SIZE;
  for (i = 0; i < n;){
    sum = sum +  array[i];
    printf( "The sum is: %d\n", sum);      
  }
  return 0;
}

it pretty much does the first part of reading out the position of the integers but it goes nuts when its time to find the sum. any help would be appreciated

this is the new code i have now

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0, n = i;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0 ){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    i++;
  }  
 
  for (i = 0; scanf("%d", &array[i]) == 0; i++){
   sum = sum +  array[i];
   printf( "The sum is: %d\n", sum);
   return (sum);
  }      
      
  return 0;
}


it doesnt find the sum. when you input 3 numbers like
1 2 3 *
it just does this

Entered integer number 0 was: 1
Entered integer number 1 was: 2
Entered integer number 2 was: 3
The sum is 1

then the program ends.
am i doing anything wrong? because the sum definitely isnt one

hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

You have to work out the simplest, clearest, and most efficient way to program.

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0, n = i;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0 ){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    sum += array[i];  //<----- add this line
    i++;
  }  
 
/* Right here, i equals the number of int's you have put into the array, so
the for loop below could be simplified n=0;n<=i;n++. However it shows an
even simpler and more efficient answer - put the sum into the same while()
loop as the entry (why the hell not?), and forget this second loop,
completely - it's useless. 
*/ 


//Ditch this loop like a bad habit :(
 for (i = 0; scanf("%d", &array[i]) == 0; i++){
   sum = sum +  array[i];
   printf( "The sum is: %d\n", sum);
   return (sum); //??? you're joking right?
  }      


  printf("The sum was %d", sum);      
  return 0;
}
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

You have to work out the simplest, clearest, and most efficient way to program.

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i = 0, n = i;
  int array[SIZE];
  int sum = 0;
  
  while (scanf("%d", &array[i]) !=0 ){
    printf("Entered Integer number %d: was %d\n", i, array[i]);
    sum += array[i];  //<----- add this line
    i++;
  }  
 
/* Right here, i equals the number of int's you have put into the array, so
the for loop below could be simplified n=0;n<=i;n++. However it shows an
even simpler and more efficient answer - put the sum into the same while()
loop as the entry (why the hell not?), and forget this second loop,
completely - it's useless. 
*/ 


//Ditch this loop like a bad habit :(
 for (i = 0; scanf("%d", &array[i]) == 0; i++){
   sum = sum +  array[i];
   printf( "The sum is: %d\n", sum);
   return (sum); //??? you're joking right?
  }      


  printf("The sum was %d", sum);      
  return 0;
}

thank you for the help. one question though, if i wanted this program to find the sum like it does in this case when its sees a non int character but not terminate, how would i do that? i mean i want it to give you the sum when its sees a character but still allow you to put more numbers in. would i have to place this program inside another loop?

hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
thank you for the help. one question though, if i wanted this program to find the sum like it does in this case when its sees a non int character but not terminate, how would i do that? i mean i want it to give you the sum when its sees a character but still allow you to put more numbers in. would i have to place this program inside another loop?

ignore that quest

hoganmadman
Newbie Poster
9 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: