0% found this document useful (0 votes)
5 views31 pages

Array

This document is a lab assignment for C programming submitted by Shikha Pandit from St. Xavier's College. It covers various topics related to arrays, including definitions, types, advantages, disadvantages, and practical C programming exercises involving arrays. The assignment includes tasks such as implementing arithmetic operations, matrix addition, and sorting algorithms.

Uploaded by

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

Array

This document is a lab assignment for C programming submitted by Shikha Pandit from St. Xavier's College. It covers various topics related to arrays, including definitions, types, advantages, disadvantages, and practical C programming exercises involving arrays. The assignment includes tasks such as implementing arithmetic operations, matrix addition, and sorting algorithms.

Uploaded by

rbmukhiya2031
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ST.

XAVIER’S COLLAGE
Maitighar, Kathmandu, Nepal
01-5344636, 01-5321365, 01-5344636
ktm@[Link]

Lab assignment number- 4


C- programing
Submitted by Submitted to Signature
Name: Shikha pandit Department of computer
Roll no: 926 science
Class: XI
Section: I
Table of Contents
1. WHAT IS ARRAY? DESCRIBE THE IMPORTANCE OF ARRAY..................................................................................1
EXPLAIN DIFFERENT TYPES OF THE ARRAY WITH THEIR SYNTAX USED FOR DECLARING AN ARRAY.......................................1
2. WRITE THE ADVANTAGES AND DISADVANTAGES OF THE ARRAY......................................................................1
3. WHY DO WE NEED AN ARRAY? IN WHAT WAY DOES A ARRAY DIFFER FROM ORDINARY VARIABLES?..........................2
4. DIFFERENTIATE BETWEEN A ONE-DIMENSIONAL AND TWO DIMENSIONAL ARRAY WITH AN EXAMPLE..........................2
5. WRITE A C PROGRAM TO IMPLEMENT THE ARITHMETIC OPERATOR (+) FOR THE GIVEN VARIABLES AND DATA TYPES....3
6. WRITE A C PROGRAM TO IMPLEMENT THE MATRIX ADDITION OF ANY TWO DIMENSIONAL ARRAY (INT DATA TYPE)......7
WRITE A C PROGRAM TO PRINT THE ELEMENTS OF A ONE DIMENSIONAL ARRAY IN SQUARE PATTERN..............................8
7. WRITE A C PROGRAM TO PRINT THE ELEMENTS OF A TWO DIMENSIONAL ARRAY IN PYRAMID PATTERN......................9
8. WRITE A C PROGRAM TO ASK TEN NUMBERS FROM THE USER AND DISPLAY THEM..............................................10
9. WRITE A C PROGRAM TO READ ‘N’ TERM NUMBERS AND TO FIND THE SUN AND AVERAGE....................................11
10. WRITE A C PROGRAM TO FIND THE GREATEST NUMBER AMONG THE FOUR NUMBERS........................................12
11. WRITE A C PROGRAM TO STORE 10 NUMBERS IN AN ARRAY AND PRINT OUT THE GREATEST NUMBER AMONG THEM.
13
12. WRITE A C PROGRAM TO INPUT 'N' NUMBERS AND FIND OUT THE LARGEST AND SMALLEST NUMBER AMONG THEM.
14
13. WRITE A C PROGRAM TO SEARCH WHETHER A NUMBER ENTERED BY THE USER IS IN THE FOLLOWING DATA-SET.....15
14. WRITE AN ALGORITHM AND C PROGRAM TO READ THE SALARIES OF 200 EMPLOYEES AND COUNT THE NUMBER OF
EMPLOYEES GETTING A SALARY OF 5000 TO 10000.............................................................................................16
15. WRITE A C PROGRAM TO READ THE AGE OF 40 STUDENTS AND COUNT THE NUMBER OF STUDENTS AGED BETWEEN
15 AND 22...................................................................................................................................................17
16. WRITE A PROGRAM USING C LANGUAGE TO READ THE AGE OF 100 PERSONS AND COUNT THE NUMBER OF
PERSONS IN THE AGE GROUP BETWEEN 50 AND 60. USE 'FOR' AND 'CONTINUE' STATEMENTS.................................18
17. WRITE A PROGRAM TO STORE THE MARK OBTAINED BY 'N' STUDENTS AND COUNT THE NUMBER OF STUDENTS WHO
OBTAINED MARKS GREATER THAN 70. ALSO, COUNT THE NUMBER OF STUDENTS WHO ARE FAILED (<35).......................19
18. WRITE A C PROGRAM THAT ASKS FOR 100 NUMBERS AND SORTS THEM IN ASCENDING ORDER............................20
19. WRITE A PROGRAM TO ASK ANY N NUMBERS FROM THE USER. SORT THEM IN ASCENDING ORDER AND DISPLAY THEM.
21
20. WRITE A C PROGRAM THAT READS THE NAME OF 100 STUDENTS AND SORT THEM IN ALPHABETICAL ORDER..........22
21. WRITE A C PROGRAM INPUT N NAMES AND SORT THEM IN ALPHABETICAL ORDER.............................................23
22. WRITE A PROGRAM ASKING ELEMENTS OF A MATRIX WITH THE SIZE 'R' BY 'C'. DISPLAY THE MATRIX IN PROPER
ORDER..........................................................................................................................................................24
23. WRITE A C PROGRAM THAT FINDS SUBTRACT OF TWO [3X3] MATRICES..........................................................25
24. WRITE A PROGRAM ASKING ELEMENTS OF A MATRIX WITH SIZE 'R' BY 'C'. FIND OUT ITS TRANSPOSE MATRIX..........26
1. What is array? Describe the importance of array.
An array is a collection of similar data type, means an array can hold value of a particular data
type for which it had been declared.
Its important as if we have a collection of similar data elements, we have inconvenient to give
each one a unique variable name. So clearly, we have a convenient way to refer to such
collection of similar data elements. And this problem is solved by the use of array by providing a
way to refer to individual items in a collection by using a single variable name .

Explain different types of the array with their syntax used for declaring an array.
The different types of array are as follow:
1. One dimension array: One dimension array stores data only row wise or column wise. It
has only one subscript i.e the number in a bracket that follows an arrays name that is used
to identify the number of individual elements in an array. The syntax used for declaring
this type of array is : data_type array_name [size of array];.
2. Two dimension array: Two dimension array stores data in both rows and columns . The
maximum capacity of 2D array is defined as the product of row size and column size. It
consists of two subscript in which first gives number of row size and second gives
number of column size. The syntax used for declaring this type of array is : data_type
array_name [row size][column size];.
3. Multi dimension array: Multi dimension array is the array which has more then one
dimension. All types of array other then 1D array are multi dimension array. The most
popular multi dimension array are 2D and 3D array. The syntax used for declaring this
type of array is : data_type array_name [size 1][size2]……..[size N].

2. Write the advantages and disadvantages of the array.


The advantages of array are :
-Arrays allow programmers to manage many related data items of the same type using a single
variable name, reducing the need for numerous individual variable declarations and resulting in
cleaner, more readable code
- They are useful for representing complex data structures like table or mathematical matrices in
a structured way.
-Arrays use memory efficiently because elements are stored sequentially in a compact manner
with no overhead for pointers between elements
-Iterating through all elements of an array using loops is straightforward and efficient due to their
sequential memory layout.
The disadvantages of array are:
-The size of an array is determined at compile time and cannot be changed during program
execution
-If an array is declared with a size larger than the number of elements actually stored, the extra
memory space is wasted.
-Arrays in C can only store elements of the same data type.
-Arrays require a single, contiguous block of memory for all their elements, so if the required
amount of contiguous memory is not available ,even if the total free memory is sufficient across
fragmented blocks, the array cannot be allocated
3. Why do we need an array? In what way does a array differ from ordinary
variables?
We need array as if we have a collection of similar data elements, we have inconvenient to give
each one a unique variable name. So clearly, we have a convenient way to refer to such
collection of similar data elements. And this problem is solved by the use of array by providing a
way to refer to individual items in a collection by using a single variable name .

A array differs from ordinary variable as in ordinary variables one data is stored in one
variable with unique element but in array multiple data of same data type can be stores in one
array with a single name.

4. Differentiate between a one-dimensional and two dimensional array with an


example.
One dimensional array Two dimensional array
It consists of only one subscript. It consists of two subscripts.
It stores data in either row wise or column It stores data in row wise and column wise.
wise
Maximum size if the size of array Maximum size if the product of row size and
column size
Syntax : data_type array_name [size of array]; Syntax: data_type array_name [row size]
[column size];
For example: int a[3]={1,2,3} For example: int a[2][2]={{1,2},
{3,4}}
5. Write a C program to implement the arithmetic operator (+) for the given variables
and data types
Normal Variable (int )
#include<stdio.h>
int main(){
int a,b,c;
printf("enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("the sum is",c);
printf("program exectued by shikha pandit");
return 0;
}

Normal Variable (char)


#include<stdio.h>
#include<string.h>
int main(){
char a[2],b[2];
printf("enter one character");
scanf("%s",&a);
printf("enter next character");
scanf("%s",&b);
strcat(b,a);
printf ("character sum is %s",b);
printf("\n program executed by Shikha Pandit");
return 0;
}
One Dimensional Array Variable (int )
#include<stdio.h>
int main(){
int a[3],b[3],c[3],i;
for(i=1;i<=3;i++){
printf("enter number");
scanf("%d",&a[i]);
}
for(i=1;i<=3;i++){
printf("enter a number");
scanf("%d",&b[i]);
}
for(i=1;i<=3;i++){
c[i]=a[i]+b[i];
}
for(i=1;i<=3;i++){
printf("%d",c[i]);
}
printf("\n program exectued by shikha pandit");
return 0;}

One Dimensional Array Variable (char )


#include<stdio.h>
#include<string.h>
int main(){
char a[50],b[50];
printf("enter one string");
scanf("%s",&a);
printf("enter next string");
scanf("%s",&b);
strcat(a,b);
printf ("sting sum is %s",a);
printf("\n program executed by Shikha Pandit");
return 0;
}

Two Dimensional Array Variable (int )


#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
printf("enter number");
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
printf("enter a number");
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=3;i++){
for(i=1;i<=3;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=1;i<=3;i++){
for(i=1;i<=3;j++){
printf("\n %d",c[i][j]);
}
}
printf("\n program exectued by shikha pandit");
return 0;
}
6. Write a C program to implement the matrix addition of any two dimensional array
(int data type).
#include<stdio.h>
int main(){
int a[3][3],c[3][3],i,j;
int b[3][3]={{2,3,6},{4,7,8},{4,6,9}};
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
printf("enter number");
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
printf("\n %d",c[i][j]);
}
}
printf("\n program exectued by shikha pandit");
return 0;
}
Write a C program to print the elements of a one dimensional array in square pattern
#include<stdio.h>
int main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
printf("* \t");
}
printf("\n");
}
printf("\n program exectued by Shikha Pandit");
return 0;
}
7. Write a C program to print the elements of a two dimensional array in pyramid
pattern
#include<stdio.h>
int main(){
int i,j;
char a[5][5]={'*'};
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("\t %s",a);
}
printf("\n");
}
printf("program executed by Shikha Pandit");
return 0;
}
8. Write a C program to ask ten numbers from the user and display them.
#include<stdio.h>
int main(){
int a[10],i;
for(i=1;i<=10;i++){
printf("enter numbers");
scanf("%d",&a[i]);
}
for(i=1;i<=10;i++){
printf("\n %d",a[i]);
}
printf("\n program executed by shikha pandit");
return 0;
}
9. Write a C program to read ‘n’ term numbers and to find the sun and average
#include<stdio.h>
int main(){
int n,i,j,a[n],b[n+1],c,d;
printf("enter how many numbers");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("enter number");
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++){
b[i+1]=a[i]+b[i];

}
d=b[n+1];
printf("\n sum is %d",d);
c=d/n;
printf("\n average is %d",c);

printf("\n program exectued by Shikha Pandit");


return 0; }

10. Write a C program to find the greatest number among the four numbers.
#include<stdio.h>
int main(){
int a[4],i;
for(i=1;i<5;i++){
printf("enter number");
scanf("%d",&a[i]);
}
if(a[1]>a[2]&&a[1]>a[3]&&a[1]>a[4]){
printf("%d is greatest",a[1]);
}
else if(a[2]>a[1]&&a[2]>a[3]&&a[2]>a[4]){
printf("%d is greatest",a[2]);
}
else if(a[3]>a[1]&&a[3]>a[2]&&a[3]>a[4]){
printf("%d is greatest",a[3]);
}
else{
printf("%d is greatest",a[4]);
}

printf("\n program exectued by Shikha Pandit");


return 0;
}
11. Write a C program to store 10 numbers in an array and print out the greatest
number among them.
#include<stdio.h>
int main(){
int a[10],i,b;
for(i=1;i<=10;i++){
printf("enter number");
scanf("%d",&a[i]);}
b=a[1];
for(i=2;i<=10;i++){
if(a[i]>b){
b=a[i];
}
}

printf("%d is greatest",b);

printf("\n program exectued by Shikha Pandit");


return 0;
}
12. Write a C program to input 'n' numbers and find out the largest and smallest
number among them.
#include<stdio.h>
int main(){
int i,b,c,n;
printf("how many numbers");
scanf("%d",&n);
int a[n];
for(i=1;i<=n;i++){
printf("enter number");
scanf("%d",&a[i]);}
b=a[1];
c=a[1];
for(i=2;i<=n;i++){
if(a[i]>b){
b=a[i];
}
if(a[i]<c){
c=a[i];
}
}

printf("%d is greatest",b);
printf("\n %d is smallest ",c);
printf("\n program exectued by Shikha Pandit");
return 0;
}
13. Write a C program to search whether a number entered by the user is in the
following data-set
Data Set are 12,45,56,67,78,560,47,78.
#include<stdio.h>
int main(){
int a[8]={12,45,56,67,78,560,47,78},b,i,c=0;
printf("enter any number");
scanf("%d",&b);
for(i=1;i<=8;i++){
if(a[i]==b){
c=1;
}}
if(c==0){
printf("%d is part of data set",b);
}
else
{
printf("%d is part of data set",b);
}
printf("\n program exectued by Shikha Pandit");
return 0;
}
14. Write an algorithm and C program to read the salaries of 200 employees and count
the number of employees getting a salary of 5000 to 10000.
Step 1: Start
Step 2: read salaries as a
Step 3: loop 200 times, is a greater then or equal to 5000 and less then or equal to 10000
If yes, increase count by 1, then go to step 2
If no, go to step 2
Step 4:display a as number of employees getting a salary of 5000 to 10000
Step 5: stop

#include<stdio.h>
int main(){
int a[200],count = 0,i;
for (i = 1; i < =200; i++) {
printf(" enter the salary ");
scanf("%d", &a[i]);
}
for (i = 1; i <= 200; i++) {
if (a[i] >= 5000 && a[i] <= 10000) {
count = count + 1;
}
}
printf("Number of employees having salaries between 5000 to 10000 is %d\n", count);
printf("\n program exectued by Shikha Pandit");
return 0;
}
15. Write a C program to read the age of 40 students and count the number of students
aged between 15 and 22.

#include<stdio.h>
int main(){
int a[40],count = 0,i;
for (i = 1; i <=40; i++) {
printf(" enter the age ");
scanf("%d", &a[i]);
}
for (i = 1;i<= 40; i++) {
if (a[i] >= 15 && a[i] <= 22) {
count = count + 1;
}
}
printf("there are %d students between the ages 15 to 22", count);
printf("\n program exectued by Shikha Pandit");
return 0;
}
[Link] a program using C language to read the age of 100 persons and
count the number of persons in the age group between 50 and 60. Use
'For' and 'Continue' statements

#include<stdio.h>
int main(){
int a[3],count = 0,i;
for (i = 1; i <=3; i++) {
printf(" enter the age ");
scanf("%d", &a[i]);
}
for (i = 1;i<= 3; i++) {
if (a[i] <50){
continue;
}
if (a[i] >60){
continue;
}
count=count+1;
}
printf("there are %d people between the ages 50 to 60", count);
printf("\n program exectued by Shikha Pandit");
return 0;
}
17. Write a program to store the mark obtained by 'n' students and count the number
of students who obtained marks greater than 70. Also, count the number of students
who are failed (<35).

#include<stdio.h>
int main(){
int n,i,count=0,c;
printf("enter the number of students");
scanf("%d",&n);
int a[n];
for(i=1;i<=n;i++){
printf("enter the mark");
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++){
if(a[i]>70){
count = count+1;
}
else if(a[i]<35){
c=c+1;
}
}
printf("there are %d students with marks greater then 70", count);
printf("\n there are %d students with marks less then 35", c);
printf("\n program exectued by Shikha Pandit");
return 0;
}
18. Write a C program that asks for 100 numbers and sorts them in ascending order

#include<stdio.h>
int main(){
int a[100][1],i,j,b,row=100;
for (i = 1; i <=row; i++) {
printf("enter number");
scanf("%d", &a[i][0]);
}
for (i = 1; i <= row-1 ; i++) {
for (j = 1; j <= row - i-1 ; j++) {
if (a[j][0] > a[j + 1][0]) {
b = a[j][0];
a[j][0] = a[j + 1][0];
a[j + 1][0] = b;
}
}
}
printf("ascending order:\n");
for (i = 1; i <= row; i++) {
printf("%d\n", a[i][0]);
}

printf("\n program exectued by Shikha Pandit");


return 0;
}
19. Write a program to ask any n numbers from the user. Sort them in ascending order
and display them.

#include<stdio.h>
int main(){
int n, i,j,b;
printf("how many numbers?");
scanf("%d",&n);
int a[n][1];
for (i = 1; i <=n; i++) {
printf("enter number");
scanf("%d", &a[i][0]);
}
for (i = 1; i <= n-1 ; i++) {
for (j = 1; j <= n - i-1 ; j++) {
if (a[j][0] > a[j + 1][0]) {
b = a[j][0];
a[j][0] = a[j + 1][0];
a[j + 1][0] = b;
}
}
}
printf("ascending order:\n");
for (i = 1; i <= n; i++) {
printf("%d\n", a[i][0]);
}

printf("\n program exectued by Shikha Pandit");


return 0;}
20. Write a C program that reads the name of 100 students and sort them in
alphabetical order.
#include <stdio.h>
#include <string.h>
int main() {
char n[5][50],b[50];
int rows = 5,i,j;
for (i = 0; i < rows; i++) {
printf("enter name ");
scanf("%s", n[i]);
}
for (i = 0; i < rows - 1; i++) {
for (j = 0; j < rows - i - 1; j++) {
if (strcmp(n[j], n[j + 1]) > 0) {
strcpy(b, n[j]);
strcpy(n[j], n[j + 1]);
strcpy(n[j + 1], b);
}
}
}
printf("\n the names in alphabetical order are as follow:");
for (i = 0; i < rows; i++) {
printf("\n%s", n[i]);
}
printf("\n program exectued by Shikha Pandit");
return 0;
}
21. Write a C program input n names and sort them in alphabetical order
#include <stdio.h>
#include <string.h>
int main() {
int rows,i,j;
printf("how many numbers?");
scanf("%d",&rows);
char n[rows][50],b[50];
for (i = 0; i < rows; i++) {
printf("enter name ");
scanf("%s", n[i]);
}
for (i = 0; i < rows - 1; i++) {
for (j = 0; j < rows - i - 1; j++) {
if (strcmp(n[j], n[j + 1]) > 0) {
strcpy(b, n[j]);
strcpy(n[j], n[j + 1]);
strcpy(n[j + 1], b);
}
}
}
printf("\n the names in alphabetical order are as follow:");
for (i = 0; i < rows; i++) {
printf("\n%s", n[i]);
}
printf("\n program exectued by Shikha Pandit");
return 0;
}
22. Write a program asking elements of a matrix with the size 'r' by 'c'. Display the
matrix in proper order.
#include <stdio.h>
int main() {
int r,c,i,j;
printf("enter the number of rows and columbs");
scanf("%d%d",&r,&c);
int a[r][c];
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("enter element");
scanf("%d", &a[i][j]);
}
}
for (i = 0; i< r; i++) {
for (j = 0; j< c; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("\n program exectued by shikha pandit");
return 0;
}
23. Write a C program that finds subtract of two [3x3] matrices.
#include <stdio.h>
int main() {
int a[3][3],b[3][3],c[3][3],i,j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("enter element of a");
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("enter elements of b");
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
c[i][j] = a[i][j] - b[i][j];

}
}
printf("the result is \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d \t", c[i][j]);
}
printf("\n");
}
printf("\n program exectued by shikha pandit");
return 0;
}

24. Write a program asking elements of a matrix with size 'r' by 'c'. Find out its
transpose matrix.
#include <stdio.h>
int main() {
int r,c,i,j;
printf("enter the number of rows and columbs");
scanf("%d%d",&r,&c);
int a[r][c],b[c][r];
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("enter element");
scanf("%d", &a[i][j]);
}
}
for (i = 0; i< r; i++) {
for (j = 0; j< c; j++) {
b[i][j]=a[j][i];
}
}
for (i = 0; i< r; i++) {
for (j = 0; j< c; j++) {
printf("%d\t", b[i][j]);
}
printf("\n");
}
printf("\n program exectued by shikha pandit");
return 0;
CONCLUSION
From this lab assignment I learned the different types of array , the use of array and how array can be
utilized with different logics to form different types of program.

You might also like