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

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

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

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

>>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.

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

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;
}

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.