Professional Documents
Culture Documents
Lab 6 PF
Lab 6 PF
Lab 6 PF
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.
Arrays can be initialized during declaration by equating the array to a listing of the array's
members in brackets. For example,
mark[3] = 9;
scanf("%d", &mark[2]);
scanf("%d", &mark[i]);
printf("%d", mark[0]);
printf("%d", mark[i-1]);
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN
Student’s tasks:
#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:
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
Ans 5:
#include <stdio.h>
int main()
{
int array[] = {1, 2, 3, 4, 5}, i, j;
int len = sizeof(array)/sizeof(array[0]);
printf("\n");
printf("Reversed array: \n");
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("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;
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]);
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: