0% found this document useful (0 votes)
12 views

C Programming Arrays

The document provides an overview of arrays in C programming, explaining their declaration, initialization, and usage for storing multiple related items. It includes examples of how to manipulate arrays, such as storing values, printing elements, and performing operations like finding averages and counting frequencies. Additionally, it presents programming exercises related to arrays, such as sorting, deleting elements, and separating odd and even integers.

Uploaded by

NIKHIL GUPTA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C Programming Arrays

The document provides an overview of arrays in C programming, explaining their declaration, initialization, and usage for storing multiple related items. It includes examples of how to manipulate arrays, such as storing values, printing elements, and performing operations like finding averages and counting frequencies. Additionally, it presents programming exercises related to arrays, such as sorting, deleting elements, and separating odd and even integers.

Uploaded by

NIKHIL GUPTA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

C Programming

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 :-

For that we use loops -

for (int h = 0; h < 60; h++)

scanf(“%d”,&score[h]);

}
To print multiple items :-

For that we use loops -

for (int h = 0; h < 60; h++)

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:

for (int h = 0; h < 60; h++)

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:

for (int h = 0; h < 20; h++)

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:

int score[5] = {75, 43, 81, 52, 68};

This declares score to be an array of size 5 and sets score[0] to 75, score[1] to 43, score[2]

to 81, score[3] to 52 and score[4] to 68.


The initial values are enclosed in braces and separated by commas. No comma is necessary

after the last value, but it is not an error to put one.

If fewer than values are supplied, then 0s would be used to fill out the array. For example,

the declaration

int score[5] = {75, 43};

sets score[0] to 75, score[1] to 43, score[2] to 0, score[3] to 0 and score[4] to 0.

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 month[] = {31,29,31,30,31,30,31,31,30,31,30,31};


This would set month[0] to 31, month[2] to 29, etc., and we would have to remember that
month[0] refers to January, month[1] refers to February, and so on. We can get around this
by using the following:

int month[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};


About Not Using Element 0
About Not Using Element 0
As we have seen, starting from element 0 can be a bit awkward and unnatural when we
have to say things like “the third element is stored in location 2”;
the subscript is “out of sync” with the position of the element. It would be much more
sensible and logical to say “the first element is stored in location 1” or “the fifth element
is stored in location 5.”
For situations like these, it is better to ignore element 0 and pretend that the subscripts
start
from 1.
However, you will have to declare the size of your array to be one more than you
actually need. For instance, if we want to cater for 60 scores, we will have to declare
score as

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 :

Print all unique elements of an array:


------------------------------------------
Input the number of elements to be stored in the array: 4
Input 4 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 2
element - 3 : 5

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 :

The Even elements are :


42 56 32
The Odd elements are :
25 47
for(i=0;i<n;i++)
#include <stdio.h> {
if (arr1[i]%2 == 0)
void main() {
{ arr2[j] = arr1[i];
int arr1[10], arr2[10], arr3[10]; j++;
int i,j=0,k=0,n; }
else
{
printf("\n\nSeparate odd and even integers in separate arrays:\n"); arr3[k] = arr1[i];
printf("------------------------------------------------------\n"); k++;
}
printf("Input the number of elements to be stored in the array :"); }
scanf("%d",&n);
printf("\nThe Even elements are : \n");
printf("Input %d elements in the array :\n",n); for(i=0;i<j;i++)
for(i=0;i<n;i++) {
{ printf("%d ",arr2[i]);
printf("element - %d : ",i); }
scanf("%d",&arr1[i]);
} printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
Write a program in C to sort elements of an array in ascending order.

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 :

Input the size of array : 5


Input 5 elements in the array in ascending order:
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
Input the position where to delete: 3

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);

printf("Enter the position to delete: ");


int pos;
scanf("%d",&pos);
int k=1,s=0;
pos=pos-1;
for(i=0;i<(n-pos);i++){
a[pos+s]=a[pos+k];
k++;
s++;
}

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 :

Input the number of elements to be stored in the array :3


Input 3 elements in the array :
element - 0 : 25
element - 1 : 12
element - 2 : 43

Expected Output :

The frequency of all elements of an array :


25 occurs 1 times
12 occurs 1 times
43 occurs 1 times
#include <stdio.h>

void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;

printf("\n\nCount frequency of each element 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]);
fr1[i] = -1;
}
for(i=0; i<n; i++)
{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++;
fr1[j] = 0;
}
}

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 –

A char name[6] = {'I','N','D','I','A'};

B char name[6] = {'I','N','D','I','A','\0'}

C char name[6] = {"I","N","D","I","A"}

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) Objects of mixed data types can be stored

b) Elements in an array cannot be sorted

c) Index of first element of an array is 1

d) Easier to store elements of same data type


What are the disadvantages of arrays?

a) Data structure like queue or stack cannot be implemented

b) There are chances of wastage of memory space if elements inserted in an


array are lesser than the allocated size

c) Index value of an array can be negative

d) Elements are sequentially accessed


A one dimensional array A has indices 1....75. Each element is a string and takes up three
memory words. The array is stored at location 1120 decimal. The starting address of A[49]
is

A. 1264

B. 1164

C. 1167

D. 1267
Arrays can be considered as a set of elements stored in
consecutive memory locations but having __________.

A. Same data type

B. Different data type

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

D. None of the above


Which of the following functions is more appropriate for reading
in a multi-word string?

A. scanf()

B. printf()

C. gets()

D. puts()

You might also like