#include<stdio.h> 
#include<string.h>         
#define SIZE 100

int main(void)

{
  int array[SIZE], i, n, j, sum=0;
                 
  printf("Enter some integers:");
  for (n=0;n<100;++n)
  {
    scanf("%d", &array[n]);
         
    if(array[n] == -1)
   {
    j=n;
    break;
   }  
	   
   j=100;
	   
   }
    for(j<=100;i>j;++i){
    printf("%d", array[n]);
   }
		
    for(j=0;j<n;++j){
    sum=sum+array[n];
    printf("The sum is %d\n", sum);
   }
  return (0);
}

Im trying to output the array on the screen and enter n values, and get them sum between -n to n ONLY. I'm doing something wrong in my for loops, but I cant figure out what. Can I just have some pointers please. Im a complete newbie at this.

Recommended Answers

All 14 Replies

}
	   	for(i=0;i>j;++i){
		printf("%d", array[n]);
	   }
		
			sum=sum+array[n];
			scanf("%d", &sum);
			printf("The sum is %d\n", sum);

What is wrong with this thing!?

Line 21 overrides any effect line 17 has. Line 24 and 25 don't make sense.

for(j<=100;i>j;++i){
printf("%d", array[n]);
}

The loop counter is i, but you are displaying the index n, isn't in the loop and doesn't vary. The initial part of the loop is "j <= 100". I'm not sure what you're trying to do there. The test is "i > j", but i is not initialized, so it's undefined behavior. You have too many loop counter variable and you are mixing them. Get rid of j altogether. You don't need it. i and n are plenty.

for(i = 0; i < n; i++)
{
    printf("%d", array[i]);
}

I imagine that's what you want in lines 24 to 26. Lines 28 to 30 look close, but there are problems. You can keep j if you want, or you can replace j with i. Agan, there's no need for a second loop variable since you have no nested loops.

For-loops look like this.

for(i = ?; i < ?; i++)
{
    // Do something with i here
}

Since you have n elements and you want to loop through them, it's what I have above. Having a "<=" operator before the first semicolon is perfectly legal, but it isn't what you want here. You usually want an "=" operator there, as above.

#include<stdio.h> 
#include<string.h>         

int main(void)

{
	int array[100], i, n, max_index, sum=0, sum1;
                 
	printf("Enter some integers:");
	for (n=0;n<100;++n)
	{
       scanf("%d", &array[n]);
         
       if(array[n] == -1)
	   {
          max_index=n;
          break;
         
	   
       max_index=100;
	   
	   }
	   	for(i=0;i<n;++i){
		printf("%d", array[i]);
		sum1=sum+array[i];
		
	   }
		
		printf("The sum is %d\n", sum1);
      	
	}
	return (0);
}

So, basically I wrote the whole thing again ... something seems wrong deffinetly:S

Here's your code in a more readable format.

#include<stdio.h> 
#include<string.h>         

int main(void)

{
    int array[100], i, n, max_index, sum=0, sum1;
                 
    printf("Enter some integers:");
    for (n=0;n<100;++n)
    {
        scanf("%d", &array[n]);
         
        if(array[n] == -1)
        {
            max_index=n;
            break;
            max_index=100;
        }
       	for(i=0;i<n;++i)
        {
            printf("%d", array[i]);
            sum1=sum+array[i];
        }
		
        printf("The sum is %d\n", sum1);
    }
    return (0);
}

Line 18 cannot be reached under any circumstances, but that's irrelevant because you never do anything with max_index, so why does it exist?

Lines 20 - 26 -- These lines execute BEFORE the user has finished entering the numbers. Do you want them to? If not, move them AFTER line 26.

Line 17 -- Once I break, I go immediately to the end of the program on line 28. This problem goes hand in hand with the other problem. Your summation loop is in the wrong spot.

Why do you have two summation variables (sum and sum1)? And sum never changes so it's not a variable really.

I did that and now it doesnt even let me enter an integer .. I'm so going down with this thing ..

I bascially did what I was told .. define size, prompt for an integer, print it, loop it, the if statement, then loop for sum, print it.

I dont get how to enter integers between -n to n .. can I do that with functions? Im lost

In simpler words please, Im new new to C, I HALF got what you said

Here's your task in generic terms.

  1. Read in all the numbers.
  2. Figure out how many numbers you read in.
  3. Find the sum of those numbers.
  4. Print it.

That's the order of the steps. The code must follow those steps. Let's pick some descriptive names. Change them if you like.

CAPACITY -- a constant. The size of the array and the maximum number of entries the user can enter.

num_entries -- The ACTUAL number of entries.

sum -- the sum of the actual number of entries.

i -- A generic loop counter variable.


Now let's look at your code and do a little substitution. I'll give you a skeleton and we'll keep what you have correctly.

#include<stdio.h> 
#include<string.h>
#define CAPACITY 100

int main(void)

{
    int array[CAPACITY];
    int i;
    int num_entries = 0;
    int sum = 0;
                 
    // Step 1 from above

    printf("Enter some integers:");
    for (num_entries = 0;num_entries <CAPACITY; ++num_entries)
    {
        scanf("%d", &array[num_entries]);
         
        if(array[num_entries] == -1)
        {
            break;
        }
    }

    // Step 2 from above
    // at this point, num_entries should be accurate.


    // Step 3 from above
    // what goes where the ? is below?
    for(i=0; i < ? ; ++i)
    {
        printf("%d", array[i]);
        sum =sum + array[i];
    }
		
    // step 4 from above
    printf("The sum is %d\n", sum1);


    return (0);
}

You pretty much had the whole thing, just a few errors and some incorrect ordering. I left a question mark for you to fill in.

#include<stdio.h>	 
#include<string.h>
#define SIZE 100

int main (void)

{
   int array[SIZE];
   int i;
   int num_entries = 0;
   int sum = 0;
	
   printf("Enter some integers:");
    for(num_entries = 0; num_entries<SIZE; ++num_entries)
    {
      scanf("%d", &array[num_entries]);
		
      if(array[num_entries] == -1 || num_entries>99)
      {
	break;
      }
		
    }
	
     for(i=0; i<num_entries; ++i)
     {
	printf("%d", array[i]);		
         sum=sum + array[i];
     }
	
     printf("The sum is %d\n", sum);
	
     return (0);
}

It's giving me a never ending list of "There are 7 odd numbersn"

Line 19 -- no need to check that num_entries isn't greater than 99. Line 15 should handle that.

"There are 7 odd numbersn" isn't anywhere in your code. Can't see how it could print that.

I did think of line 19 being a possibility of that, and I tried without it as well, but the outcome is the same

>> but the outcome is the same

The outcome is "There are 7 odd numbersn"?

umm okay so I copy/pasted on another page, but my output is still weird

Enter some integers:5
7
3
8
2
26
2643
7
2
9
-1
57382262643729The sum is 2712
Press Enter to return to Quincy...

The output looks spot-on to me. The only thing that looks "weird" is that all your entries run into each other because you don't separate them with spaces, tabs, or newlines. Change line 28 to this and test to make sure everything looks OK.

printf("%d\n", array[i]);

what if I want to print the output horizontally (in one line) rather than each integer in a different line .. I dont need line 28 for that ..

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.