0% found this document useful (0 votes)
12 views50 pages

Array Final

The document provides an overview of arrays in C programming, explaining the differences between simple and array variables, how to declare and initialize arrays, and methods for storing and manipulating array values. It also covers searching techniques, memory management, and practical programming examples such as sorting and counting frequencies of elements. Additionally, it includes questions and answers related to arrays to reinforce understanding.

Uploaded by

khushi75v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views50 pages

Array Final

The document provides an overview of arrays in C programming, explaining the differences between simple and array variables, how to declare and initialize arrays, and methods for storing and manipulating array values. It also covers searching techniques, memory management, and practical programming examples such as sorting and counting frequencies of elements. Additionally, it includes questions and answers related to arrays to reinforce understanding.

Uploaded by

khushi75v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

C Programming

Arrays
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 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.
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};
Search in Array
Linear search
int n = sizeof(arr) / sizeof(arr[0]);

•sizeof(arr): This returns the total size in bytes of the array arr[]. sizeof() is an operator in C
that returns the size of its operand in bytes.
•sizeof(arr[0]): This returns the size in bytes of the first element of the array arr[].
•Since arr[] is an array of integers, arr[0] represents the first element, and sizeof(arr[0])
returns the size of an integer in bytes.
•sizeof(arr) / sizeof(arr[0]): This division calculates how many elements can fit in the array arr[].
•By dividing the total size of the array by the size of one element, we get the number of elements in the
array.
•int n = ...: This assigns the result of the division to the variable n, which will hold the number of
elements in the array.
Binary Search
Array and memory
•"Address of arr[%d]: %p\n": This is the format string used by printf. It contains two format
specifiers:
•%d: This specifier is for printing integers. It is replaced by the value of i,
which represents the index of the element in the array.
•%p: This specifier is for printing memory addresses. It is replaced by the memory address
contained
in the variable address_of_element.
•\n: This is the newline character, which adds a newline after the address is printed.
•i: This is the index of the element in the array. It will replace %d in the format string.
•(void*)address_of_element: This part casts the integer memory address stored in
address_of_element to a void* pointer type.
The (void*) cast is used because the %p format specifier expects a void* argument.
This ensures that the memory address is properly interpreted and printed by printf.
•%p: This specifier prints the memory address. It is replaced by the value of
(void*)address_of_element.
3 .Write a program in C to read n number of values in an array and display it
in reverse order.
Write a program in C to copy the elements of one array into another array.
Array with Pointer
Program to Swap Elements of an Array
using Pointers:
Program to Find the Sum of Elements
using Pointers:
2D-Array-Initializing and Accessing Elements
Finding the Sum of Elements
Transposing a Matrix
Multiplying Two Matrices
Finding the Address of a Specific Element
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
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 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