C Programming Arrays
C Programming Arrays
Arrays
By S.K. Ansari
Simple vs Array Variable
● The variables we have been using so far (such as ch, n, sum) are normally called
simple variables. We know these can store one item of data: for instance, one
number or one character. Of course, the value stored in the variable can be
changed, if we wish.
● However, there are many situations in which we wish to store a group of related
items and to be able to refer to them by a common name. The array variable allows
us to do this.
Array Declaration
● Before an array is used, it must be declared. For example, consider the statement:
int score[60];
● Array subscripts start at 0 and go up to n-1, if n is the size of the array.
● Like a simple variable, when an array is declared, the values of its elements remain
undefined until we execute statements that store values in them.
Store Values in an Array
Consider the array score. If we wish, we could set selected
elements to specific values, as follows:
score[3] = 56;
score[7] = 81;
But what if we wish to set the 60 locations to 60 scores? Would
we have to write 60 statements as in the following?
score[0] = 45;
score[1] = 63;
score[2] = 39;
..
To store multiple items :-
scanf(“%d”,&score[h]);
}
To print multiple items :-
printf(“%d”,score[h]);
what is important is the value of the subscript, not the variable that is used as the subscript.
If we want to set the elements to a different value (-1, say), we could write:
score[h] = -1;
It should be noted that even though we have declared score to be of size 60, it is not required that we use all
the elements. For example, suppose we want to set just the first 20 elements of score to 0, we could do this
with the following:
score[h] = 0;
his sets elements score[0], score[1], score[2], up to score[19] to 0. Elements score[20] to score[59] remain
undefined. C provides another way of initializing an array – in its declaration. Consider this:
This declares score to be an array of size 5 and sets score[0] to 75, score[1] to 43, score[2]
If fewer than values are supplied, then 0s would be used to fill out the array. For example,
the declaration
If more than 5 values are supplied, you would get a warning or an error, depending on
your compiler setting. For example, the following will generate a warning or error since there are
8 values:
int score[5] = {75, 43, 81, 52, 68, 49, 66, 37};
It is possible to omit the size of the array and write, for example, this:
int score[] = {75, 43, 81, 52, 68, 49, 66, 37};
In this case, the compiler counts the number of values to determine the size of the array.
Here, the number of values is 8, so it is the same as if we had written this declaration:
int score[8] = {75, 43, 81, 52, 68, 49, 66, 37};
As another example, suppose we wanted to store the number of days in a month in a leap
year. We could use this:
int score[61];
1. Write a program to scan an array and print
an array.
2. Write a program to scan an array of numbers and find
average and diff from average of each number.
SOLUTION :-
if (n == 0) printf("No numbers entered\n");
else {
printf("\nNumbers entered: %d\n", n);
//find average and difference from average
printf("Sum of numbers: %1.0f\n\n", sum);
#include <stdio.h>
double average = sum / n;
#define MaxNum 100
printf("The average is %3.2f\n", average);
int main() {
printf("\nNumbers and differences from
int a, num[MaxNum];
average\n");
int n = 0;
for (int h = 0; h < n; h++)
double sum = 0;
printf("%4d %6.2f\n", num[h], num[h] -
printf("Enter up to %d numbers (end with 0)\n", MaxNum);
average);
scanf("%d", &a);
}
while (a != 0) {
}
sum += a;
num[n++] = a; //store in location n, then add 1 to n
scanf("%d", &a);
}
3 .Write a program in C to read n number of values in an array and display it
in reverse order.
Ques 4 . Write a program in C to find the sum of all elements of an array.
Write a program in C to copy the elements of one array into another array.
Write a program in C to print all unique elements in an array.
Test Data :
Expected Output :
The unique elements found in the array are:
35
#include <stdio.h>
int main()
{
int arr1[100], n,ctr=0;
int i, j, k;
printf("\n\nPrint all unique elements of an array:\n");
printf("------------------------------------------\n");
printf("Input the number of elements to be stored in the array: ");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("\nThe unique elements found in the array are: \n");
for(i=0; i<n; i++)
{
ctr=0;
for(j=0,k=n; j<k+1; j++)
{
/*Increment the counter when the seaarch value is duplicate.*/
if (i!=j)
{
if(arr1[i]==arr1[j])
{
ctr++;
}
}
}
if(ctr==0)
{
printf("%d ",arr1[i]);
}
}
printf("\n\n");
}
Write a program in C to separate odd and even integers into separate arrays.
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
Expected Output :
Test Data :
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9
Expected Output :
Elements of array in sorted ascending order:
24579
Write a program in C to delete an element at a desired position from an array.
Test Data :
Expected Output :
The new list is : 1 2 4 5
//Question deleter postion
#include<stdio.h>
int main(){
int i,b,n,sum=0,j,count;
printf("Enter length of your array");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
printf("Enter element no . %d",i);
scanf("%d",&a[i]);
}
printf("The array values are:\n");
for(i=0;i<n;i++){
printf("%d",a[i]);
}
printf("You have entered %d values\n",n);
for(i=0;i<(n-1);i++){
printf("%d ",a[i]);
}
}
Write a program in C to count the frequency of each element of an array.
Test Data :
Expected Output :
void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;
if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\nThe frequency of all elements of array : \n");
for(i=0; i<n; i++)
{
if(fr1[i]!=0)
{
printf("%d occurs %d times\n", arr1[i], fr1[i]);
}
}
1. What is Array?
A. Collection of different type of elements
B. Collection of similar type of elements
C. None of the above
D. Both A and C
What is right way to Initialize
array?
A int num[6] = { 2, 4, 12, 5,
45, 5};
B int n{} = { 2, 4, 12, 5, 45,
5 };
C int n{6} = { 2, 4, 12 };
D int n(6) = { 2, 4, 12, 5, 45,
5 };
What will be printed after execution of the following code?
void main()
{
int arr[10] = {1,2,3,4,5};
printf("%d", arr[5]);
}
A Garbage Value
B 5
C 6
D 0
5) What will be the output of the program ?
#include<stdio.h>
void main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
}
A 3, 2, 15
B 2, 3, 20
C 2, 1, 15
D 1, 2, 5
#include <stdio.h>
int main(void)
{
char p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf + 1)[5];
printf("%d", p);
return 0;
}
5
6
9
Error
An array elements are always stored in ________ memory locations.
A Sequential
B Random
C Sequential and Random
D None of the above
Size of the array need not be specified, when
A Initialization is a part of definition
B It is a declaratrion
C It is a formal parameter
D All of these
What will be the output of the following code?
void main()
{
int a[10];
printf("%d %d", a[-1], a[12]);
}
A 00
B Garbage value 0
C 0 Garbage Value
D Garbage vlaue Garbage Value
In C Programming, If we need to store word "INDIA" then syntax is as below –
D name = "INDIA"
What will be the output of the program ?
#include<stdio.h>
int main()
{
int a[5] = {51, 1, 5, 20, 25};
int x, y, z;
x = ++a[1];
y = a[1]++;
z = a[x++];
printf("%d, %d, %d", x, y, z);
return 0;
}
A 2, 3, 20
B 2, 1, 5
C 1, 2, 5
D 3, 2, 5
What are the advantages of arrays?
A. 1264
B. 1164
C. 1167
D. 1267
Arrays can be considered as a set of elements stored in
consecutive memory locations but having __________.
C. Same scope
D. None of these
15. Array is an example of _______ type memory allocation.
A. Compile time
B. Run time
C. Both A and B
A. scanf()
B. printf()
C. gets()
D. puts()