Thread: Struggle in changing the code

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    1

    Lightbulb Struggle in changing the code

    Good morning,

    How to change the following code
    Code:
    #include<stdio.h>
    int main()
    {
       int a[100], n, i, psum=0, nsum=0;
    
       printf("Size od the table [1-100]: ");
       scanf("%d",&n);
       printf("Write %d elements: ",n);
    
       for(i=0; i<n; i++)
       {
         scanf("%d",&a[i]);
         if(a[i]<0) nsum += a[i];
         else psum += a[i];
       }
    
       printf("Sum of positive numbers: %d",psum);
       printf("\nSum of negative sumbers: %d",nsum);
       return 0;
    }
    so I don't need to write the size of the board and I can just write and write elements until I write 0, so then it fnishes?
    And how I can print all positive elements and all negative elements??

  2. #2
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    For printing the negatives of the array just run the array again and check if the number is negative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struggle in understanding
    By clau in forum C Programming
    Replies: 3
    Last Post: 03-23-2016, 08:22 AM
  2. merge arrays homework struggle
    By baldysm in forum C Programming
    Replies: 2
    Last Post: 02-08-2014, 12:07 PM
  3. The BSOD struggle continues.
    By Aran in forum Tech Board
    Replies: 17
    Last Post: 06-03-2006, 12:51 PM
  4. The great struggle for random quotes!
    By Jeremy G in forum Game Programming
    Replies: 2
    Last Post: 01-26-2003, 01:28 PM
  5. Newbie's struggle.
    By anoopks in forum Linux Programming
    Replies: 1
    Last Post: 11-12-2002, 02:38 PM

Tags for this Thread