Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*4. write a program which will accept both positive and negative integers
- from the user until 999 is entered to stop the prog.
- sum up both sets of num separately and determine the smallest and largest num from both sets, display these results*/
- #include <stdio.h>
- //int sum_minus(*,int); //prototype for sum of minus array
- //int sum_plus(*int, int); //prototype for sum of plus array
- int sum(int *a, int);
- main()
- {
- int num, plus[99] = {0}, minus[99]= {0}, count = 0, array_amount, minus_total, plus_total, *a, total;
- printf("Enter 999 to exit.\n\n");
- do{ //start of request loop
- printf("Enter an integer, negative or positive:\n");
- scanf("%i",&num);
- /*procedure to fill array*/
- if(num >= 0)
- {
- plus[count] = num ;
- ++count;
- }
- else
- {
- minus[count] = num ;
- ++count ;
- }
- }
- while( num != 999 );
- array_amount = count ;
- a = plus ;//assign to pointer for function call
- plus_total = sum(a, array_amount) - 999 ;
- a = minus ; //assign to pointer for function call
- minus_total = sum(a, array_amount) ;
- //printout of totals
- printf("The sum of negative integers is %i.\n", minus_total);
- printf("The sum of positive integers is %i.\n\n", plus_total);
- minus_total > plus_total ? printf("%i is a larger number than %i.", minus_total, plus_total) : printf("%i is a larger number than %i.", plus_total, minus_total) ;
- }
- //int sum_minus(*a,b)
- int sum(int *a, int array_amount)
- {
- int b, total = 0;
- for (b = 0; b <= array_amount; b++ )
- {
- total+=a[b];
- } ;
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement