Lab 6 PF

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

Lab 6

Objective:

Arrays in C

Theory:
Array: A collection of individual values, all of the same data type, stored in adjacent memory
locations. One Dimensional Array: An array with a single variable index. Using the array name
together with an integral valued index in square brackets refers to the individual values. The first
array element always has the subscript 0. The second array element has the subscript 1, etc.
The base address of an array is its beginning address in memory.

Declaring an Array: Use the following syntax below.


DataType Array Name [ConstIntExpression];
The example below shows the declaration of an integer array of size 10 with element 0 - 9.

const int MAXSIZE = 10;


int array[MAXSIZE];

Arrays can be initialized during declaration by equating the array to a listing of the array's
members in brackets. For example,

int array[MAXSIZE] = {2 , 4, 6, 8, 10, 12, 14, 16, 18, 20};

Insert and Print Array Elements

int mark[5] = {19, 10, 8, 17, 9}

// insert different value to third element

mark[3] = 9;

// take input from the user and insert in third element

scanf("%d", &mark[2]);

// take input from the user and insert in (i+1)th element

scanf("%d", &mark[i]);

// print first element of an array

printf("%d", mark[0]);

// print ith element of an array

printf("%d", mark[i-1]);
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

Student’s tasks:

1. Program to read and print the elements of array.

#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
printf("\nElements in array are: ");
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

Ans 1:

OUTPUT:

2. Program to find the average of n (n < 10) numbers using arrays.


#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}
Ans 2:

OUTPUT:

3. This program prompts the user for a number from the given data searches and returns
its location.

void main(void)
{
clrscr();
int a[5]={8,6,3,7,2},vl,a1,a2=0;
for(a1=0;a1<=4;a1++)
printf("%d ",a[a1]);
printf("\nFrom the above enter one value to search : ");
scanf("%d",&vl);
while(a[a2]!=vl)
{
a2++;
}
printf("Location of %d is %d",vl,a2+1);
getch();
}
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

Ans 3:

OUTPUT:

4. This program sorts the numbers inside the array in ascending order.

void main(void)
{
clrscr();
int tmp,arr[5]={23,16,97,33,42};
for(int a=0;a<5;a++) /* sort them using a bubble sort. Repeating loop 5(# of
elements in array) times */
{
for(int b=0;b<4;b++) /* loop for comparison 4 times(5-1 times)*/
{
if(arr[b]>arr[b+1]) /* compare adjacent elements */
{
/* exchange element */
tmp=arr[b];
arr[b]=arr[b+1];
arr[b+1]=tmp;
}
}
}
for(int c=0;c<5;c++) /* display sorted list */
printf("\n%d",arr[c]);
getch();
}

Ans 4:

OUTPUT:
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

5. Write a program in C to read n number of values in an array and display it in reverse


order.

Ans 5:

#include <stdio.h>
int main()
{
int array[] = {1, 2, 3, 4, 5}, i, j;
int len = sizeof(array)/sizeof(array[0]);

printf("Original array: \n");

for (i = 0; i < len; i++)


printf("%d ", array[i]);

printf("\n");
printf("Reversed array: \n");

for (j = len-1; j >= 0; j--)


printf("%d ", array[j]);

return 0;
}
OUTPUT:

6. Write a program that takes input for array int a[5] and array int b[5] and exchanges
their values.
Ans 6:

#include<stdio.h>
int main() {
int arra[5],arrb[5],arrc[5],i;

printf("Enter 5 elements for array # 1:\n");


for (i=0;i<5;i++)
scanf("%d",&arra[i]);

printf("Enter 5 elements for array # 2:\n");


for (i=0;i<5;i++)
scanf("%d",&arrb[i]);
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

printf("\nArrays before swapping:\n");

printf("Array # 1: ");
for (i=0;i<5;i++)
printf("%d",arra[i]);

printf("\nArray # 2: ");
for (i=0;i<5;i++)
printf("%d",arrb[i]);

for (i=0;i<5;i++) {
arrc[i]=arra[i];
arra[i]=arrb[i];
arrb[i]=arrc[i];
}
printf("\n\nArrays after swapping:");
printf("\nArray # 1: ");
for (i=0;i<5;i++)
printf("%d",arra[i]);

printf("\nArray # 2: ");
for (i=0;i<5;i++)
printf("%d",arrb[i]);

return 0;
}
OUTPUT:
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

7. Write a program that takes 10 integers as input and prints the largest integer and its
location in the array.

Ans7:

#include<stdio.h>
int main()
{
int i, index, b;
float arr[10], a;

printf("Please enter ten numbers:\n");

for (i = 0; i < 10; ++i)


{
scanf("%f", &arr[i]);
}
int max = arr[0];
for (i = 1; i < 10; i++)
{
if (arr[i] > max)
{
a = arr[i];
b = i;
}
}
printf("Largest element = %.2f at index %d", a, b);
return 0;
}
OUTPUT:

8. Write a program to copy the contents of one array into another in the reverse order.

Ans 8:
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

Source Code:
#include<stdio.h>
int main()
{
int arra[10], arrb[10], i, j;
printf("\nEnter 10 integers: ");
for (i = 0; i<10; i++)
scanf("%d", &arra[i]);

for (i = 0, j = 9; i<10; i++, j--)


arrb[i] = arra[j];

printf("\nArray after being copied in reverse order: ");


for (i = 0; i<10; i++)
printf("%d ", arrb[i]);

return 0;
}
OUTPUT:

9. Write a program to find the transpose of a matrix (order of matrix given by user).

Ans 9:

#include <stdio.h>
int main(){
int m, n, i, j, matrix[10][10], transpose[10][10];
printf("Enter rows and columns :\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (i= 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0;i < m;i++)
for (j = 0; j < n; j++)
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i< n; i++) {
for (j = 0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
return 0;
}

OUTPUT:

You might also like