Guest User

Lab 8 exploring arrays

a guest
May 4th, 2013
199
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Lab 8 - Part 1 - Exploring Arrays
  2.    *** Your Name Here ***
  3. */
  4. #include <stdio.h>
  5. #define MAX_SIZE  10  /* Maximum number of items in table */
  6.  
  7. /* Fill the table with the desired number of items */
  8. void fillTable(int list[],  /* output - list of n integers */
  9.                int size)    /* input  - number of items to fill */
  10. {
  11.    int index;
  12.    for (index = 0; index < size; index++)
  13.    {
  14.          list[index] = index * index * index;
  15.    }
  16. }
  17.                      
  18. /* Display the table of cubes */
  19. void showTable(int list[],  /* input - list of n integers */
  20.                int size)    /* input  - number of items to show */
  21. {
  22.    /* Find sum of cubes. */
  23.    int sumCubes = 0;
  24.    int index;
  25.    for (index = 0; index < size; index++)
  26.    {
  27.          sumCubes += list[index];
  28.    }
  29.  
  30.    /* Display each cube and the sum of all cubes. */
  31.    printf("N\tCUBE\n");
  32.    for (index = 0; index < size; index++)
  33.    {
  34.          printf("%d \t  %d \n", index, list[index]);
  35.    }
  36.    printf("\nSum of cubes is %d\n", sumCubes);
  37.  
  38. }
  39.  
  40. /* Obtain input from user and call helper functions */
  41. int main(void)
  42. {
  43.    int cubes[MAX_SIZE];  /* an array of integers */
  44.    int numItems;         /* desired number of items in list */
  45.    
  46.    printf("How many numbers should be cubed? ");
  47.    scanf("%d", &numItems);
  48.    
  49.    fillTable(cubes, numItems);  
  50.    showTable(cubes, numItems);
  51.    return 0;
  52. }
RAW Paste Data