Creates two eight-element arrays of doubles - C Array

C examples for Array:Array Element

Introduction

Uses a loop to let the user enter values for the eight elements of the first array.

Set the elements of the second array to the cumulative totals of the elements of the first array.

Demo Code

#include <stdio.h>  

#define SIZE 8  /*from  w  w  w  .  ja  v a2  s . c  o  m*/

int main(void)  
{  
    double arr[SIZE];  
    double arr_cumul[SIZE];  
    int i;  
      
    printf("Enter %d numbers:\n", SIZE);  
      
    for (i = 0; i < SIZE; i++)  
    {  
        printf("value #%d: ", i + 1);  
        scanf("%lf", &arr[i]);  
    }  
    arr_cumul[0] = arr[0];      /* set first element */  
    for (i = 1; i < SIZE; i++)  
        arr_cumul[i] = arr_cumul[i-1] + arr[i];  
      
    for (i = 0; i < SIZE; i++)  
        printf("%8g ", arr[i]);  
    printf("\n");  
    for (i = 0; i < SIZE; i++)  
        printf("%8g ", arr_cumul[i]);  
    printf("\n");  
      
                  
    return 0;  
}

Result


Related Tutorials