Array
The array can be defined as a number of memory locations, each of which can store the same types & which can be reference through the same variable. It will start from zero to nth. We denote to define multi-variable of same data type.
One dimensional array Declaration
<datatype><variable name>[size];
Int n [5];
Flowchart
Program
Question: write a program to read user input integers and display them with their address.
#include <stdio.h>
#include <conio.h>
void main()
{
int sub[5]; // creating 5 memory location i.e. 0, 1, 2, 3, 4.
int total = 0, a;
printf("enter a value: ");
scanf("%d", &a);
printf("enter the %d numbers to see their locations: \n", a);
// loop to accept a user input number
for (int i = 0; i < a; i++)
{
scanf("%d", &sub[i]);
total = total + sub[i];
}
// loop to display the address of an array.
for (int i = 0; i < a; i++)
{
printf("n[%d] = %d \n", i, sub[i]);
}
getch();
}
OUTPUT
enter a value: 5
enter the 5 numbers to see their locations:
1 4 7 8 9
n[0] = 1
n[1] = 4
n[2] = 7
n[3] = 8
n[4] = 9
0 Comments