Guest User

Untitled

a guest
Mar 12th, 2012
204
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*4. write a program which will accept both positive and negative integers
  2. from the user until 999 is entered to stop the prog.
  3. sum up both sets of num separately and determine the smallest and largest num from both sets, display these results*/
  4.  
  5. #include <stdio.h>
  6.  
  7. //int sum_minus(*,int); //prototype for sum of minus array
  8. //int sum_plus(*int, int); //prototype for sum of plus array
  9.  
  10. int sum(int *a, int);
  11.  
  12. main()
  13. {
  14. int num, plus[99] = {0}, minus[99]= {0}, count = 0, array_amount, minus_total, plus_total, *a, total;
  15.  
  16. printf("Enter 999 to exit.\n\n");
  17. do{ //start of request loop
  18. printf("Enter an integer, negative or positive:\n");
  19. scanf("%i",&num);
  20.  
  21. /*procedure to fill array*/
  22.  
  23. if(num >= 0)
  24. {
  25. plus[count] = num ;
  26. ++count;
  27. }
  28. else
  29. {
  30. minus[count] = num ;
  31. ++count ;
  32. }
  33. }
  34. while( num != 999 );
  35.  
  36. array_amount = count ;
  37.  
  38. a = plus ;//assign to pointer for function call
  39.  
  40.  
  41. plus_total = sum(a, array_amount) - 999 ;
  42.  
  43. a = minus ; //assign to pointer for function call
  44. minus_total = sum(a, array_amount) ;
  45.  
  46.  
  47.  
  48. //printout of totals
  49.  
  50. printf("The sum of negative integers is %i.\n", minus_total);
  51. printf("The sum of positive integers is %i.\n\n", plus_total);
  52.  
  53. 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) ;
  54. }
  55.  
  56. //int sum_minus(*a,b)
  57. int sum(int *a, int array_amount)
  58. {
  59. int b, total = 0;
  60. for (b = 0; b <= array_amount; b++ )
  61. {
  62. total+=a[b];
  63. } ;
  64. return total;
  65.  
  66. }
RAW Paste Data