# Chapter 6 ARRAYS IN C

1. Define array. Why do we use arrays in computer programs.
Ans:-Array is a collection of similar data items where elements are stored in contiguous memory locations in our computer. Each data item of an array is typically called an element and each element has a specific memory location and this can be accessed by its relative position in the array. This relative position can also be referred as the index of the element.
We use arrays in computer programs because arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
2. Can we store both integer and float types of data in a single array? Demonstrate this by writing a simple C program.
Ans:- No we cannot store both integer and float types of data in a single array.

``````#include<stdio.h>
int main()
{
int arr;
printf("Enter the array elements:");
for(int i=0;i<=2;i++){
scanf(" %d",&arr[i]);
}
printf("The array elements are:");
for(int i=0;i<=2;i++){
printf("%d",arr[i]);
printf(" ");
}
return 0;
}``````

In the above program, when array elements are taken as integer the program prints the correct output. But if we take the input as float values(like 3.14,2.54,5.7,……..) an error will occur. Hence, we cannot store both integer and float types of data in a single array.

3. Write the indices of the first and last element of the following array declaration.

char city =(‘S’, ‘I’, ‘L’, ‘C’, ‘H’, ‘A’, ‘R’, };
Ans:- The indices of the first element i.e.S is 0 and the indices of the last element i.e.R is 6.

 ARRAY ELEMENT S I L C H A R INDICES 0 1 2 3 4 5 6

4. Write a C program and declare an integer type array with capacity 7. Take input to the array from the keyboard. Display the 8th element of the array. Hint: display num if num is the name of the array. Analyze the output.
Ans:-
Code:-

``````#include<stdio.h>
int main()
{
int num;
printf("Enter  array elements:");
for(int i=0;i<7;i++){
scanf(" %d",&num[i]);
}
printf("The 8th array elements is:");

printf("%d",num);

return 0;
}``````

Output:-
Enter  array  elements:1
2
3
4
5
6
7
The 8th array element is: 7

5. Write a C program and declare an integer type array with 7 elements in it. Display the address of the individual elements in the array.
Ans:-

``````#include<stdio.h>
int main()
{
int arr={1,2,3,4,5,6,7};
for(int i=0;i<7;i++){
printf("The array element is %d",arr[i]);
printf(" and the index of element is %d",i);
printf("\n");
}
return 0;
}``````

Output:-

The array element is 1 and the index of element is 0
The array element is 2 and the index of element is 1
The array element is 3 and the index of element is 2
The array element is 4 and the index of element is 3
The array element is 5 and the index of element is 4
The array element is 6 and the index of element is 5
The array element is 7 and the index of element is 6

6. Write a C program and declare two integer type arrays, each with capacity 7. Take input only to the first array. Write a loop to copy the elements of the first array to the second one. Display the elements of the second array.
Coding:-

``````#include<stdio.h>
int main()
{
int num1,num2;
//taking input of 1st array i.e. num1
printf("Enter the array elements of 1st array is :-");
for(int i=0;i<7;i++){
scanf("%d",&num1[i]);
}
//copying num1 into num2
for(int i=0;i<7;i++){
num2[i]=num1[i];
}
//printing values of num2
printf("The array elements of 2nd array copied from 1st array are:- ");
for(int i=0;i<7;i++){
printf("%d",num2[i]);
printf(" ");
}
return 0;
}``````

7. Write a strategy to find the summation of all the even numbers stored in an array. Write a C program for the same. If the array elements are {1, 2, 4, 3, 5, 6, 7, 7, 8), the output of the program will be 20.

Strategy:
Step1: Declare an integer array with sufficient capacity.

``int num;``

Step2: Take input to the array elements.

``int num={1,2,4,3,5,6,7,7,8};``

Step3 : Check whether the number is even or odd i.e if the number is completely divisible by 2 then it is even.

``if(num[i]%2==0)``

Step4 : If it is even calculate summation.

``sum=sum+num[i];``

Step5: Declaration of the result.

``printf("\n The summation is: %d",sum);``

Coding:-

``````#include<stdio.h>
int main()
{
int num={1,2,4,3,5,6,7,7,8};
int sum=0;
for(int i=0;i<9;i++){
if(num[i]%2==0){
sum=sum+num[i];
}
}
printf("\n The summation is: %d",sum);
return 0;
}``````

8. Write a strategy to find the summation of all the even positioned numbers stored in an array. Write a C program for the same. If the array elements are (1, 2, 4, 3, 5, 6, 7, 7, 8), the output of the program will be 18.

Strategy:
Step1: Declare an integer array with sufficient capacity.

``int num;``

Step2: Take input to the array elements.

``int num={1,2,4,3,5,6,7,7,8};``

Step3 : Add all the even positioned numbers.

``````for(int i=0;i<=9;i++){
sum=sum+num[i+1];
i++;
}``````

Step4: Declaration of the result.

``printf("\n The summation is: %d",sum);``

Coding:-

``````#include<stdio.h>
int main()
{
int num={1,2,4,3,5,6,7,7,8};
int sum=0;
for(int i=0;i<=9;i++){
sum=sum+num[i+1];
i++;
}
printf("\n The summation is: %d",sum);
return 0;
}``````

9. Write the logic to replace only the first occurrence of an element in an array. For example, if the array elements are (1, 2, 3, 4, 5, 1, 2, 3) and we want to replace element 3 by 0, the array content becomes (1, 2, 0, 4, 5, 1, 2, 3). Write a complete C program incorporating the logic.

Logic:- Our first job is to find the index of the element that is to be replaced. For this,we go through the array element and we make a comparison. When we get a match, we replace the new element in that position using the index and get out of the loop. This will replace the first occurrence of the given element.
Coding:-

``````#include<stdio.h>
int main()
{
int t=8,m,n;
int num={1,2,3,4,5,1,2,3};
printf("\n The elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
printf("\n Replacing Elements......");
printf("\n Enter old number you want to replace:-");
scanf("%d",&n);
printf("\n Enter the new number:- ");
scanf("%d",&m);
for(int j=0;j<t;j++){
if(num[j]==n){
num[j] =m;
printf("\n Number is replaced");
break;
}
}
printf("\n Now the elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
return 0;
}``````

10. Write the logic to replace only the last occurrence of an element in an array. For example, if the array elements are {1, 2, 3, 4, 5, 1, 2, 3) and we want to replace element 3 by 0, the array content becomes {1, 2, 3, 4, 5, 1, 2, 0}. Write a complete C program incorporating the logic.

Logic:- Our first job is to find the index of the element that is to be replaced. For this,we go through the array element in reverse order and we make a comparison. When we get a match, we replace the new element in that position using the index and get out of the loop. This will replace the first occurrence of the given element.
Coding:-

``````#include<stdio.h>
int main()
{
int t=8,m,n;
int num={1,2,3,4,5,1,2,3};
printf("\n The elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
printf("\n Replacing Elements......");
printf("\n Enter old number you want to replace:-");
scanf("%d",&n);
printf("\n Enter the new number:- ");
scanf("%d",&m);
for(int j=t-1;j>0;j++){
if(num[j]==n){
num[j] =m;
printf("\n Number is replaced");
break;
}
}
printf("\n Now the elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
return 0;
}``````

11. Write a C program to replace all the even positioned elements in an integer array by 0. For example, if the array elements are (1, 2, 3, 9, 5, 5, 7, 1, 9), it becomes (1, 0, 3, 0, 5, 0, 7, 0, 9).

Coding:-

``````#include<stdio.h>
int main()
{
int num,t,m,n;
printf("\n Enter size of array :-");
scanf("%d",&t);
printf("\n Enter the elements of array :-");
for(int i=0;i<t;i++){
scanf("%d",&num[i]);
printf(" ");
}
printf("\n The elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
for(int j=0;j<t;j++){
num[j+1] =0;
j++;
}
printf("\n Number are replaced");
printf("\n Now the elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
return 0;
}``````

12. Write a strategy to replace all the odd numbers in an integer array by 0. For example, if the array elements are (1, 2, 3, 9, 5, 5, 7, 1, 9), it becomes (0, 2, 0, 0, 0, 0, 0, 0, 0). Write a C program for the same.

Coding:-

``````#include<stdio.h>
int main()
{
int num,t,m,n;
printf("\n Enter the number of elements to be entered in array :-");
scanf("%d",&t);
printf("\n Enter the elements of array :-");
for(int i=0;i<t;i++){
scanf("%d",&num[i]);
printf(" ");
}
printf("\n The elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
for(int j=0;j<t;j++){
if(num[j]%2!=0){
num[j]=0;
}
}
printf("\n Now the elements of array are:-");
for(int i=0;i<t;i++){
printf("%d",num[i]);
printf(" ");
}
return 0;
}``````

13. Write a C prog to store your name and your mothers name in two different strings. Display them one after another.

Code:

``````#include <stdio.h>
int main() {
char s1, s2;
int length, j;
gets(s1);
gets(s2);
length = 0;
while (s1[length] != '\0') {
++length;
}
for (j = 0; s2[j] != '\0'; ++j, ++length) {
s1[length] = s2[j];
}
s1[length] = '\0';
printf("After concatenation: ");
puts(s1);
return 0;
}``````

14. Write a C program to declare 3 integer type arrays to store the marks of 10 students scored in 3 different subjects. Take another integer to store the average marks of the students. The average mark of a student is the average of the marks scored by the student in 3 subjects. This should be calculated and inserted by the program. Then you should display the average marks of the students. The program should also display “PASS” or “FAIL” along with the average as per the rule: PASS if average >= 45, FAIL otherwise.
Coding:-

``````#include<stdio.h>
int main()
{
int s1,s2,s3;
printf("\n Enter marks for each of those 10 students :");
for(int i=0;i<3;i++){
printf("\n Enter marks of 1st Subject: ");
scanf("%d",&s1[i]);
printf("\nEnter marks of 2nd Subject: ");
scanf("%d",&s2[i]);
printf("\nEnter marks of 3rd Subject: ");
scanf("%d",&s3[i]);
}
for(int i=0;i<3;i++){
float avg=(s1[i]+s2[i]+s3[i])/3;
if(avg>45){
printf("\n Pass ,Average is :%f" ,avg);
}
else{
printf("\n Fail ,Average is :%f" ,avg);
}
}
return 0;
}
``````

15. Write any two limitations of arrays. Can you store your name and roll number in the same array?

Ans:-The two limitations of arrays are:-
i). Fixed size

ii). Homogenous data

Fixed size: We have already seen that before using an array, we must declare it with a size. This is the capacity of the array. If some requirement arises (at run time)n to store more elements in that array, we cannot do so. For instance, in an integer array with size 10, we cannot store 11 integers.

Homogeneous data: We know that an array is a collection of similar types of data. We cannot store a float variable in an integer array.
No,we cannot store our name and roll in same array because, an array can store only homogeneous data. As name is a string and roll is an integer type data so we can’t store them in same array.