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

Array Assignment 7 C

This document is an assignment from St. Xavier's College, Kathmandu, focusing on arrays in C programming. It includes theoretical questions, algorithms, pseudocode, flowcharts, and source code for various tasks related to arrays, such as finding the greatest number, calculating sums and averages, and sorting numbers. The assignment is submitted by Ishan Panta to Jamuna Ma'am in the Computer Science department.

Uploaded by

chhetriishan57
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)
5 views38 pages

Array Assignment 7 C

This document is an assignment from St. Xavier's College, Kathmandu, focusing on arrays in C programming. It includes theoretical questions, algorithms, pseudocode, flowcharts, and source code for various tasks related to arrays, such as finding the greatest number, calculating sums and averages, and sorting numbers. The assignment is submitted by Ishan Panta to Jamuna Ma'am in the Computer Science department.

Uploaded by

chhetriishan57
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

ST.

XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU
PHONE: 01-5344636,01-5321365
EMAIL: ktm@[Link]

ASSIGNMENT NUMBER – 7
Array in C

Submitted by: Submitted to: Signature


Name: Ishan Panta Department of
Roll no: 809 Computer Science
Class:11 Jamuna Ma’am
Section: H
Table of Contents
Theoretical Questions ................................................................................................1
1. To ask ten numbers from the user and display them. .........................................4
2. To read ‘n’ term numbers and to find the sum and average. ..............................7
3. To find the greatest number among the four numbers ......................................10
4. To store 10 numbers in an array and print out the greatest number among
them. .........................................................................................................................13
5. To input ‘n’ numbers and find out the largest and smallest number among
them. .........................................................................................................................14
6. To search whether a number entered by the user is in the following data set or
not. 16
7. To read the salaries of 8 employees and count the number of employees
getting a salary of 5000 to 10000. ............................................................................18
8. To read the age of 8 students and count the number of students aged between
15 and 22. .................................................................................................................19
9. To read the age of 8 persons and count the number of persons in the age group
between 50 and 60. Use ‘For’ and ‘Continue’ statements. ......................................20
10. To store the mark obtained by ‘n’ students and count the number of students
who obtained marks greater than 70 and who are failed (<35). ..............................21
11. To ask for 8 numbers and sorts them in ascending order. .............................22
12. To ask any n numbers from the user and sort them in ascending order and
display them. ............................................................................................................24
13. To read the name of 8 students and sort them in alphabetical order. ............26
14. To input n names and sort them in alphabetical order. .................................28
15. To ask elements of a matrix with the size ‘r’ by ‘c’ and display the matrix in
proper order. .............................................................................................................30
16. To find the subtract of two [3*3] matrices. ...................................................32
17. To find the transpose of the matrix with size ‘r’ by ‘c’. ................................34
Conclusion ...............................................................................................................36
Theoretical Questions

1. What is an array? Describe the importance of an array.


Ans- An array is the type of variable having more than one number of spaces or
the values to be stored and with multiple dimensions too. The importance of an
array is that with using only one variable name we can store many values in it. It
is especially important when the similar type of values is need to be stored with
same name. for instance, values related to salaries of an employees. Not only this,
array also eliminates the redundancy of declaring multiple variables and saving
our precious time.

2. Explain different types of the array with their syntax used for declaring an
array.
The different types of the array with their syntax on the basis of number of
dimensions is given below:
Single dimension -A one-dimensional array stores data in a linear form.
data_type array_name[size];
Two dimension- A two-dimensional array stores data in rows and columns
(matrix form). data_type array_name[rows][columns];

Multi-dimension or n dimension- An array having more than two dimensions.


data_type array_name[size1][size2][size3];

1
3. Write the advantages and disadvantages of the array.
The advantages and disadvantages of the array are given below:
Advantages-

a. Stores multiple values using a single variable name.


b. Makes data handling easy and efficient.
c. Reduces code size by using loops.
d. Allows random access to elements.
e. Useful in mathematical and scientific applications.

Disadvantages-

a. Size of an array is fixed.


b. Wastage of memory may occur.
c. Cannot store different data types in the same array.
d. Insertion and deletion of elements is difficult.
e. Requires contiguous memory.

4. Why do we need an array? In what way does an array differ from ordinary
variables?

We need an array to store and process large amounts of similar data efficiently.
Without arrays, we would need many separate variables, which makes programs
complex and difficult to manage.

5. Difference between array and ordinary variables:

Ordinary variable Array

1. Stores one value Stores multiple values

2. Uses a single memory location Uses multiple contiguous memory


locations
3. Accessed directly Accessed using index

4. Suitable for small data Suitable for large data

2
6. Differentiate between a one-dimensional and two-dimensional array with an
example.

One-dimensional array Two-dimensional array


1. Stores one value Stores data in tabular form

2. Stores data in linear form Uses two indices

3. Uses one index Matrix-like structure

4. Simple structure Syntax: a[3][3]


Syntax: a[10] Example: int m[2][2];
Example: int a[5]

3
1. To ask ten numbers from the user and display them.

Algorithm:
Step 1: Start
Step 2: Declare an integer array a of size 10.
Step 3: Display the message “Give ten numbers”
Step 4: Repeat steps 5–6 for i = 0 to 9.
Step 5: Read an integer value and store it in a[i]
Step 6: Increment i.
Step 7: Display the message “The given numbers are”
Step 8: Repeat steps 9–10 for j = 0 to 9.
Step 9: Display the value of a[j].
Step 10: Increment j.
Step 11: Display the message “executed by Ishan Panta”.
Step 12: Stop.

Pseudocode:
START
DECLARE integer array a[10]
PRINT "Give ten numbers"
FOR i ← 0 TO 9
READ a[i]
END FOR
PRINT "The given numbers are"
FOR j ← 0 TO 9

4
PRINT a[j]
END FOR
PRINT "executed by Ishan Panta"
STOP

Flow chart:

5
Source code:
#include<stdio.h>
int main() {
int a[10];
printf("Give ten numbers:\n");
for(int i=0; i<10; i++){
scanf("%d",&a[i]);
}printf("The given numbers are:");
for(int j=0; j<10; j++){
printf("%d ",a[j]);
}
printf("\nexecuted by Ishan Panta");
return 0;
}

Output:

6
2. To read ‘n’ term numbers and to find the sum and
average.

Algorithm:
Step 1: Start
Step 1: Declare i, j, n, a[n] and s.
Step 1: Display the message” give the no. of numbers:”
Step 4: Read the value n.
Step 4: Display the message “enter the numbers:”
Step 5: Repeat step 6 and 7 from i=0 to i<n.
Step 6: read the values and input in a[i].
Step 7: increment i.
Step 8: Repeat step 9 and 10 from j=0 to j<n.
Step 9: Sum s and a[j] and assign in s
Step 10: increment j
Step 11: Calculate average = s / n
Step 12: Print “the sum of the given numbers s and its average s/n.
Step 13: Print “executed by Ishan Panta”.
Step 14: End

Pseudocode:
Start
Declare i, j, n, a[n] and s.
Print” give the no. of numbers:”
Read n

7
Print” enter the numbers:”
For i=0 to n-1Read a[i]
End for
For j=0 to n-1
S = s+a[j]
End for
average = s / n
print “The sum of the given numbers is ", s, " and its
average is ", average
print “Executed by Ishan Panta"
stop

Flow chart:

8
Source code:
#include<stdio.h>
int main(){
int s, n, i, j, a[n];
printf("give the no. of numbers:");
scanf("%d",&n);
printf("enter the numbers:");
for( i=0; i<n; i++){
scanf("%d",&a[i]);
}
for( j=0; j<n; j++){
s+=a[j];
}

printf("the sum of the given numbers is %d and its average is %d\n",s,s/n);


printf("executed by Ishan Panta\n");
return 0;
}
Output:

9
3. To find the greatest number among the four numbers

Algorithm:
Step 1: Start
Step 2: Declare i, j, a[4].
Step 3: Print “Enter the four numbers:".
Step 4: Repeat the statement ‘read a[i]’ for four times.
Step 5: Declare m=a[0].
Step 6: Repeat step 7 from j=0 to 3.
Step 7: check a[j]>a[0] if true then m=a[j].
Step 8: Print “The largest number among four numbers is: m”.
Step 9: Print “Executed by Ishan Panta".
Step 10: End

Pseudocode:
Start
Declare i, j, a[4].
Print “Enter the four numbers:".
For i=0 to 3
Read a[i]
Declare m=a[0].
For j=0 to 3
If a[j]>a[0] then m=a[j].
Print “The largest number among four numbers is: m”
Print “Executed by Ishan Panta”

10
Stop

Flow chart:

11
Source code:
#include<stdio.h>
int main() {
int i,j, a[4];
printf("Enter the four numbers:");
for(i=0;i<4;i++){
scanf("%d",&a[i]);
}
int m=a[0];
for(j=0;j<4;j++){
if(a[j]>a[0]){
m=a[j];
}}
printf("The largest number among four numbers is: %d\n",m);
printf("executed by Ishan Panta");
return 0;
}
Output:

12
4. To store 10 numbers in an array and print out the greatest
number among them.

Source code:
#include<stdio.h>
int main() {
int i,j, a[10];
printf("Enter the 10 numbers:");
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
int m=a[0];
for(j=0;j<10;j++){
if(a[j]>a[0]){
m=a[j];
}}
printf("The greatest number among 10 numbers is: %d\n",m);
printf("executed by Ishan Panta");
return 0;
}
Output:

13
5. To input ‘n’ numbers and find out the largest and smallest
number among them.
Source code:
#include<stdio.h>
int main() {
int i,j,n;
printf("Enter the total number of terms:");
scanf("%d",&n);
int a[n];
printf("Enter the numbers: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
int m=a[0],l=a[0];
for(j=0;j<n;j++){
if(a[j]>a[0]){
m=a[j];
}
if(a[j]<a[0]){
l=a[j];
}}
printf("The greatest number among 10 numbers is: %d and the smallest
number is %d\n",m,l);
printf("executed by Ishan Panta");
return 0;
}
14
Output:

15
6. To search whether a number entered by the user is in the
following data set or not.

Source code:
#include <stdio.h>
int main() {
int i, n;
int a[8] = {12, 45, 56, 67, 78, 560, 47, 78};
int f = 0;
printf("Enter the number: ");
scanf("%d", &n);
for (i = 0; i < 8; i++) {
if (n == a[i]) {
f = 1;
break;
}
}
if (f)
printf("Yes, the number is in the given data set");
else
printf("No, the number is not in the given data set");
printf("\nexecuted by Ishan Panta");
return 0;
}

16
Outputs:

17
7. To read the salaries of 8 employees and count the number
of employees getting a salary of 5000 to 10000.

Source code:
#include<stdio.h>
int main(){
int s=0,i, a[8];
printf("Enter the salaries of the employees:");
for(i=0;i<8;i++){
scanf("%d",&a[i]);
if(a[i]>=5000&&a[i]<=10000){
s+=1;
}
}
printf("The number of employees getting a salary of 5000 to 10000
is: %d",s);
printf("\nexecuted by Ishan Panta");
return 0;
}
Output:

18
8. To read the age of 8 students and count the number of
students aged between 15 and 22.
#include<stdio.h>
int main(){
int s=0,i, a[8];
printf("Enter the ages of the students:");
for(i=0;i<8;i++){
scanf("%d",&a[i]);
if(a[i]>=15&&a[i]<=22){
s+=1;
}
}
printf("The number of students aged between 15 and 22 is: %d",s);
printf("\nexecuted by Ishan Panta");
return 0;
}

Output:

19
9. To read the age of 8 persons and count the number of
persons in the age group between 50 and 60. Use ‘For’
and ‘Continue’ statements.

Source code:
#include <stdio.h>
int main(){
int age[8];
int i,count=0;
for(i=0;i<8;i++){
scanf("%d",&age[i]);
if(age[i]<50||age[i]>60)
continue;
count++;
}
printf("Count = %d",count);
printf("\nexecuted by Ishan Panta");
return 0;
}
Output:

20
10. To store the mark obtained by ‘n’ students and count
the number of students who obtained marks greater than
70 and who are failed (<35).
Source code:
#include <stdio.h>
int main(){
int n,i,above=0,fail=0;
scanf("%d",&n);
int m[n];
for(i=0;i<n;i++){
scanf("%d",&m[i]);
if(m[i]>70)
above++;
if(m[i]<35)
fail++;
}
printf("Above 70 = %d\nFailed = %d",above,fail);
printf("\nexecuted by Ishan Panta");
return 0;
}
Output:

21
11. To ask for 8 numbers and sorts them in ascending
order.
Source code:
#include <stdio.h>
int main(){
int a[8][1];
int i,j,temp;
for(i=0;i<8;i++){
scanf("%d",&a[i][0]);
}
for(i=0;i<8;i++){
for(j=i+1;j<8;j++){
if(a[i][0]>a[j][0]){
temp=a[i][0];
a[i][0]=a[j][0];
a[j][0]=temp;
}
}
}
for(i=0;i<8;i++){
printf("%d ",a[i][0]);
}
printf("\nexecuted by Ishan Panta");
return 0;
}

22
Output:

23
12. To ask any n numbers from the user and sort them in
ascending order and display them.
Source code:
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n][1];
int i,j,temp;
for(i=0;i<n;i++){
scanf("%d",&a[i][0]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i][0]>a[j][0]){
temp=a[i][0];
a[i][0]=a[j][0];
a[j][0]=temp;
}
}
}
for(i=0;i<n;i++){
printf("%d ",a[i][0]);
}
printf("\nexecuted by Ishan Panta");

24
return 0;
}

Output:

25
13. To read the name of 8 students and sort them in
alphabetical order.
Source code:
#include <stdio.h>
#include <string.h>
int main(){
char name[8][50],temp[50];
int i,j;
for(i=0;i<8;i++){
scanf("%s",name[i]);
}
for(i=0;i<8;i++){
for(j=i+1;j<8;j++){
if(strcmp(name[i],name[j])>0){
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
for(i=0;i<8;i++){
printf("%s\n",name[i]);
}
printf("\nexecuted by Ishan Panta");
return 0;

26
}
Output:

27
14. To input n names and sort them in alphabetical order.

Source code:
#include <stdio.h>
#include <string.h>
int main(){
int n;
scanf("%d",&n);
char name[n][50],temp[50];
int i,j;
for(i=0;i<n;i++){
scanf("%s",name[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(name[i],name[j])>0){
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
for(i=0;i<n;i++){
printf("%s\n",name[i]);
}

28
printf("\nexecuted by Ishan Panta");
return 0;
}

Output:

29
15. To ask elements of a matrix with the size ‘r’ by ‘c’ and
display the matrix in proper order.

Source code:
#include <stdio.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
int a[r][c];
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nexecuted by Ishan Panta");
return 0;
}

30
Output:

31
16. To find the subtract of two [3*3] matrices.

Source code:
#include <stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3];
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
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("%d ",c[i][j]);
}
printf("\n");
}
printf("\nexecuted by Ishan Panta");

32
return 0;
}
Output:

33
17. To find the transpose of the matrix with size ‘r’ by ‘c’.

Source code:
#include <stdio.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
int a[r][c];
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
for(j=0;j<c;j++){
for(i=0;i<r;i++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nexecuted by Ishan Panta");
return 0;
}

34
Output:

35
Conclusion

After completing these questions, I have gained a clear understanding of how arrays
work in the C programming language. By using single-dimensional arrays, I learned
how to store multiple values, perform operations like finding the sum, average,
largest and smallest numbers, and search for specific data. These exercises also
helped me understand how loops make programs shorter, faster, and easier to
manage.
Working with two-dimensional arrays improved my knowledge of matrices. I
learned how to read matrix elements, display them properly, sort data, subtract
matrices, and find the transpose of a matrix. These tasks helped me understand how
data can be organized in rows and columns and how logical thinking is important
while writing programs.
Overall, these questions strengthened my basic programming skills and improved
my confidence in writing C programs. I also learned the importance of correct
syntax, proper use of conditions, and clear logic. This practice will be very helpful
for solving real-world problems and for further studies in programming.

36

You might also like