C Program to Calculate Sum of Array Elements
Array is a mile stone of C Program which we may use almost everywhere. Here is a basic program to understand array, memory locations and structure of array etc.
What is an array?
An array is a group of continuous memory locations which we can access by the address. An array of a particular datatype can only store values of that data type(often it convert and use the given data).
Array initialization
The syntax of array initialization in C is :
Algorithm
Here the array named 'array' of type 'int' is used to store different numbers and adding them by separately accessing the array elements using for loop.
//Program
//Program
#include <stdio.h<
voide main()
{
int n, i;
float num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n <= 0)
{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
return sum;
}
What is an array?
An array is a group of continuous memory locations which we can access by the address. An array of a particular datatype can only store values of that data type(often it convert and use the given data).
Array initialization
The syntax of array initialization in C is :
datatype array_name[size];
Algorithm
Here the array named 'array' of type 'int' is used to store different numbers and adding them by separately accessing the array elements using for loop.
//Program
//Program
#include <stdio.h<
voide main()
{
int n, i;
float num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n <= 0)
{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
return sum;
}
Comments
Post a Comment