0% found this document useful (1 vote)
96 views21 pages

Department of Electrical Engineering

This document is a lab report for an experiment on implementing arrays in C programming. It includes 10 tasks where the student wrote C code to: 1) Read and print data in a 1D array; 2) Print the first 10 natural numbers in an array; 3) Find the sum and average of elements in an array; 4) Make a number pyramid pattern; 5) Display terms and sum of a harmonic series; 6) Declare and initialize a 2D 4x4 array; 7) Initialize a 2D array to zero; 8) Display a 2D array; 9) Find the sum of the first row of a 2D array; 10) Find the sum of the third column of a 2D array;

Uploaded by

Noman Khan
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 (1 vote)
96 views21 pages

Department of Electrical Engineering

This document is a lab report for an experiment on implementing arrays in C programming. It includes 10 tasks where the student wrote C code to: 1) Read and print data in a 1D array; 2) Print the first 10 natural numbers in an array; 3) Find the sum and average of elements in an array; 4) Make a number pyramid pattern; 5) Display terms and sum of a harmonic series; 6) Declare and initialize a 2D 4x4 array; 7) Initialize a 2D array to zero; 8) Display a 2D array; 9) Find the sum of the first row of a 2D array; 10) Find the sum of the third column of a 2D array;

Uploaded by

Noman Khan
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
You are on page 1/ 21

Department of Electrical Engineering

Faculty of Engineering & Applied Sciences


Riphah International University, Islamabad, Pakistan

Program: B.Sc. Electrical Engineering Semester: II


Subject: SEL-201 Data Structure & Algorithm Date: …………….
Experiment 3: Implementation of Arrays (single & two dimensional) by executing C programs.
CLO1 (P2)

Name: Urooj Abid Roll No: 13777

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to 5 Organization/Structure 5
conduct
Experiment &
Tool usage
Implementation 5 5
and Results Data Presentation
Total Marks obtained

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Introduction:
In this lab we have studied about arrays .Array is a collection of items stored at contiguous memory locations.
The idea is to combine multiple items of the same type.A one-dimensional array is a type of linear array. A one-
dimensional array is like a list. Accessing its elements involves a single subscript that can represent a row or
column index.A two-dimensional array is a type such as int or string, with two pairs of square brackets . 2D row
elements are arranged in rows and columns. And for 2D rows, the new operator specifies both the number of rows
and the number of columns.

Lab Task:

Lab Task 1: Read data into the array: write a program to input 10 numbers using the array.
Code
#include <stdio.h>
void main()
{ int a[10];
int i;
printf("Enter 10 elements in the array:\n");
for(i=0; i<10; i++)
{ printf("Element %d: ",i+1);
scanf("%d",&a[i]);
}
printf("Elements in array are: ");
for(i=0; i<10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
}

Output
Lab Task 2: Print array: Write a program to display the first 10 natural numbers using array.
Code
#include <stdio.h>
int main()
{
int a[10];
int i;
printf("Elements in array are:\n");
for(i=0; i<10; i++)
{
a[i]=i+1;
printf("%d\n",a[i]);
}
return 0;
}

Output
Lab Task 3: Sum and average all elements: Write a program to find the sum and average of
n numbers using the array.
Code
#include <stdio.h>
int main()
{ int I ,n;
float s = 0.0, a;
printf ("Enter the value of n \n");
scanf("%d", &n);
int array[n];
printf("Enter %d numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Array elements \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{ s+=array[i];}

a = s / n;
printf("\n Sum of all numbers = %.2f\n", s);
printf("\n Average of all numbers = %.2f\n", a);
return 0;
}
Output

Lab Task 4: Write a program in C to make such a pattern like a pyramid with numbers
increased by 1.
1
23
456
7 8 9 10
Code

#include <stdio.h>
#include<stdlib.h>
int main()
{

int N=4;
int S;
int T;
int J;
int count=1;
float I;
for(S=1; S<=N; S++)
{
for(J=I; J<=(N-S);J++)
{
printf(" ");
}
for(T=1; T<=S; T++)
{
printf("%d ", count++);
}
printf("\n");
}

return 0;
}

Output

Lab Task 5: Write a program in C to display the n terms of harmonic series and their sum. 
A=1+ 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Code
#include <stdio.h>
int main()
{
int i,n;
float s=0.0;
printf("Enter the number of terms : ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
if(i<n)
{
printf("1/%d + ",i);
s+=1/(float)i;
}
if(i==n)
{
printf("1/%d ",i);
s+=1/(float)i;
}
}
printf("\nSum of Series upto %d terms : %f \n",n,s);
return 0;
}
Output

Lab Task 6: Declare an array of type int of size 4 by 4.


0 1 2 3
0

Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
Output
Lab Task 7:
Initialize all location of the array to zero by the following command.
i.e int A[4][4]={0};
Program Code:
#include<stdlib.h>
#include<stdio.h>
int main()
{
int i[4][4];
int S;
int T;
for(S=0; S<4; S++)
{
for(T=0; T<4; T++)
{
printf("Enter Value:",S,T);
scanf("%d",&i[S][T]);
}
}
printf("Tow dimensional array element:\n");
for(S=0;S<4;S++)
{
for(T=0; T<4; T++)
{
printf("%d",i[S][T]);
if (T==3)
{
printf("\n");
}
}
}
return 0;
}
output

Lab Task 8: Display the above array by following code:


That is:
for (row = 0; row < NUMBER_OF_ROWS; row++)
{
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
printf (setw(5));
printf (matrix[row][col] );
}
Code

Output
Lab Task 9: Write a code that finds the sum of the first row's elements.

0 1 2 3
0
1
2
3

Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
int sum=0;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0; i<1; i++)
{
for(j=0; j<4; j++)
{
sum=sum+a[i][j];
}
}
printf("Sum of 1st Row is: %d\n",sum);
}
Output
Lab Task 10: Write a code that finds the sum of the third column's elements.
0 1 2 3
0

Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
int sum=0;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}

printf("\n");
}
for(i=0; i<4; i++)
{
for(j=2; j<3; j++)
{
sum=sum+a[i][j];
}
}
printf("Sum of 3rd Column is: %d\n",sum);
}
Output

Lab Task 11: Write a code that finds the sum of all elements of the array.

Code
#include <stdio.h>
void main()
{
int a[4][4];
int i,j;
int sum=0;
printf("Enter 10 elements in the array:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("Element %d,%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Elements in array are:\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
sum=sum+a[i][j];
} } printf("Sum of All Elements is: %d\n",sum);
}

Output
Lab Task 12: Write a code that finds the greater number from the whole array.
Code
#include <stdio.h>
int main()
{
int i;
int arr[5];
printf("Enter Elements of Array: \n");
for (i=0; i<5; i++)
{

printf("Enter Element %d: ", i+1);


scanf("%d",&arr[i]);
}
for (i=1; i<5; i++)
{
if (arr[0] < arr[i])
{
arr[0] = arr[i];
}
}
printf("Largest element = %d", arr[0]);
return 0;
}
Output

Conclusion:
The main conclusion of this lab experiment is that we have learned how to implements arrays in c
programing. Arrays are mostly and easily used to solve the basic daily life problem very easily by programming.

You might also like