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

Array

Arrays allow storing multiple values in a single variable. To create an array, specify the data type and name followed by square brackets. Values are inserted using a comma-separated list within curly braces. Array elements are accessed using their index number, which starts from 0. Several programs are presented to demonstrate common array operations like inputting/outputting elements, calculating average, finding largest element, separating even/odd numbers into arrays, and sorting arrays. Multidimensional arrays can represent tables with multiple rows and columns. Sample programs demonstrate initializing and manipulating 2D arrays.

Uploaded by

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

Array

Arrays allow storing multiple values in a single variable. To create an array, specify the data type and name followed by square brackets. Values are inserted using a comma-separated list within curly braces. Array elements are accessed using their index number, which starts from 0. Several programs are presented to demonstrate common array operations like inputting/outputting elements, calculating average, finding largest element, separating even/odd numbers into arrays, and sorting arrays. Multidimensional arrays can represent tables with multiple rows and columns. Sample programs demonstrate initializing and manipulating 2D arrays.

Uploaded by

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

Arrays

• Arrays are used to store multiple values in a single variable, instead of


declaring separate variables for each value.

• To create an array, define the data type (like int) and specify the name
of the array followed by square brackets [].

• To insert values to it, use a comma-separated list, inside curly braces:

int myNumbers[] = {25, 50, 75, 100};


Array Indexing
Accessing Array elements
• To access an array element, refer to its index number.

• Array indexes start with 0: [0] is the first element. [1] is the second
element, etc.

• This statement accesses the value of the first element [0] in


myNumbers:

• Example :
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);

// Outputs 25
Program 1
• Write a program to take 5 values from the user and store them in an
array. Print the elements stored in the array
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Program 2
• Write a program to find the average of n numbers using arrays.
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0;
double average;
printf("Enter number of elements: ");
scanf("%d", &n);

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


printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);

// adding integers entered by the user to the sum variable


sum += marks[i];
}
// explicitly convert sum to double
// then calculate average
average = (double) sum / n;
printf("Average = %.2lf", average);
return 0;
}
Program 3
• Write a program to find the largest number in an array.

#include <stdio.h>
int main()
{
int a[]={10,2,4,11,9},i;
int G=a[0];
for(i=1;i<5;i++)
{
if(G<a[i])
G=a[i];
}
printf(“The largest number is %d ", G);
return 0;
}
Program 4
• Write a program to extract even numbers and create a separate array
from the given array.
Ex: Input : A1[5]={3,4,5,2,10} #include <stdio.h>
Output : A2[3]={4,2,10} int main()
{
int A1[10]={0,2,4,11,8},i, j=0, A2[5];
for(i=0;i<5;i++)
{
if(A1[i]%2==0)
{
A2[j]=A1[i];
j++;
}
}
for(int i=0;i<j;i++)
printf("%d ", A2[i]);
return 0;
}
Program 5
• Write a program to extract numbers in the array within a given range.
Ex : Input : A1[5]={2, 4,6, 8, 10}, lower limit : 5, upper limit : 9
Output : {6,8}
Sample Program 6
• Write a program to separate even and odd numbers from an array into
two different arrays.
Ex : Input : A[]={3,2,4,6,7,5}
Output : A1[]={2,4,6} A2[]={3,7,5}
Program 7
• Write a program to arrange the elements of an array in ascending
order.
Program 8
• Write a program to find the union of two arrays.
• Ex : Input : A1[]={1,2,5,6,8} A2[]={1,3,5,4,7,8}
• Output : {1,2,3,4,5,6,7,8]}
Program 9
• Write a program to find the intersection of two arrays.
Ex : Input : A1={1,2,5,6,8} A2={1,3,5,4,7,8}
• Output : {1,5,8}
Program 10
• Write a program to remove the duplicate items in the given array.
Ex : Input : A1[]={1,2,1,4,1,4,2,3}
Output : A2[]={1,2,4,3}
Program 11
• Write a program to request the user to enter a decimal number and
display its equivalent binary number.
Ex : Input : 9
Output : 1001
Multidimensional Arrays
• n C programming, you can create an array of arrays. These arrays are
known as multidimensional arrays. For example,
float x[3][4];
• Here, x is a two-dimensional (2d) array. The array can hold 12 elements.
You can think the array as a table with 3 rows and each row has 4
columns.
Multidimensional Arrays
• Initialization of a 2d array
• // Different ways to initialize two-dimensional array

int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[][3] = {{1, 3, 0}, {-1, 5, 9}};

int c[2][3] = {1, 3, 0, -1, 5, 9};


Multidimensional Arrays
• Initialization of a 3d array
• You can initialize a three-dimensional array in a similar way to a two-
dimensional array.

int test[2][3][4] =
{ {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}} };
Sample Program 12
• Write a program to read a 2*2 matrix elements through user input and
print them in the matrix form .
Sample Program 13
• Write a program to read a 2*3 matrix elements through user input and
print the transpose of the matrix.
Sample Program 14
• Write a program to find the sum two matrices.
Sample Program 15
• Write a program to find multiplication of two matrices.

You might also like