Advertisement
Guest User

Witch

a guest
Mar 22nd, 2010
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. /* Sum numbers 0 ... 9 two at a time            */
  2. /* index increments                             */
  3. /* sum() function uses "pass by reference"      */
  4. /* even if it's only about one "return value"   */  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9.  
  10. void sum(int, int, int * );
  11.  
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. //    int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  16. //    int array_copy[10];
  17.     int * array = NULL;
  18.     int * array_copy = NULL;
  19.  
  20.     int i, j, n;
  21.  
  22. /* ___________________________________________________________________________*/
  23. /* Input */
  24.  
  25.     printf("How many numbers do you want to input? --> "); scanf("%d",&n);
  26.     // calloc = Memory allocation for array.
  27.     array = calloc(n, sizeof *array);
  28.  
  29.     for(i=0 ; i<n ; i++)
  30.     {
  31.         printf("\n\nInput a number, for element number <%d>. --> ", i);
  32.         scanf("%d", array+i);
  33.     }
  34.  
  35.     // Confirm input values.
  36.     for(i=0 ; i<n ; i++)
  37.         printf("\n< Element number %d > , = %d", i, array[i]);
  38.  
  39.         printf("\n\n");
  40.         printf("______________________________________________________________");
  41.         printf("\n\n");
  42.  
  43. /* ___________________________________________________________________________*/
  44. /* Copy array */
  45.  
  46.     for(i=0; i<n; i++)
  47.         array_copy[i] = array[i];
  48.  
  49.     for(j=0; j<n; j++)
  50.         printf("%d\n", array_copy[j]);
  51.  
  52.     printf("\n");
  53.     printf("__________________________________________");
  54.     printf("\n\n");
  55.  
  56.  
  57. /* Sum */
  58. /*
  59.     for( i=0; i<n; i++)
  60.     {
  61.         sum(array[i], array[i+1], &array[i+1]);
  62.         printf("Partial sum %d equals: %d\n", i+1, array[i+1]);    
  63.     }
  64.    
  65.     printf("\nGrandtotal equals: %d\n", array[9]);
  66.  
  67.     printf("\n");
  68.     printf("__________________________________________");
  69.     printf("\n\n");
  70.  
  71.     for(j=0; j<n; j++)
  72.         printf("%d\n", array_copy[j]);
  73. */
  74.  
  75.     free(array);
  76.     free(array_copy);
  77.  
  78.     system("PAUSE");   
  79.     return 0;
  80. }
  81.  
  82.  
  83. /*************
  84. * Functions
  85. *************/
  86. /* ___________________________________________________________________________*/
  87.  
  88. void sum(int a, int b, int *summa )
  89. {
  90.     *summa = a + b;  /* return with "pass by reference" */          
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement