Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

What is Arrays?

-An array is a collection of items of same data type stored at


contiguous memory locations.
This makes it easier to calculate the position of each element by
simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by
the name of the array). The base value is index 0 and the
difference between the two indexes is the offset.

Give the two types of Arrays.


One dimensional array.
Multi-dimensional array.

Give example source code of each type.


one-dimensional array
#include<stdio.h>
int main()
{
int arr[5], i, s = 0;
for(i = 0; i < 5; i++)
{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}
for(i = 0; i < 5; i++)
{
s += arr[i];
}
printf("\nSum of elements = %d ", s);
return 0;
}

Multi-dimensional array.
#include <stdio.h>

int main () {

int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};


int i, j;

for ( i = 0; i < 5; i++ ) {


for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}

return 0;
}

What is Strings?
-String in C programming is a sequence of characters terminated
with a null character ‘\0’. Strings are defined as an array of
characters. The difference between a character array and a string
is the string is terminated with a unique character ‘\0’.

Give an example of a strings used in C Language.


char greetings[] = "Hello World!";
char str[] = "String";

You might also like