Q9 Write A Program To Take User Name, Id, Department, 5 Subjects Marks and Make A Marksheet of It
Q9 Write A Program To Take User Name, Id, Department, 5 Subjects Marks and Make A Marksheet of It
Q9 Write A Program To Take User Name, Id, Department, 5 Subjects Marks and Make A Marksheet of It
#include<stdio.h>
void main(void)
{
int marks,a,b,h=0; float per; char c[50],d[15],e[100]; clrscr();
printf("\n\t----------\n\tMARKSHEET\n\t----------");
printf("\n\n\tENTER YOUR NAME\t"); gets(c);
printf("\n\tENTER YOUR ID\t"); gets(d);
printf("\n\tENTER YOUR DEPARTMENT NAME\t"); gets(e);
for(a=1;a<=5;a++)
{ printf("\n\tENTER THE MARKS OF %d SUBJECT\t",a);
scanf("%d",&b); h=b+h; }
marks=h/5; per=h/5; clrscr();
printf("\n\t----------\n\tMARKSHEET\n\t----------");
printf("\n\tNAME :\t%s",c);
printf("\n\tID :\t%s",d);
printf("\n\tDEPARTMENT :\t%s",e);
printf("\n\n\tMARKS OBTAINED %d OUT OF 500",h);
printf("\n\tPERCENTAGE : %.1f ",per);
if(marks >= 90 && marks <=100)
{ printf("\n\tGrade A+"); }
else if(marks>=80 && marks<90)
{ printf("\n\tGrade A"); }
else if(marks>=70 && marks<80)
{ printf("\n\tGrade B"); }
else if(marks>=60 && marks<70)
{ printf("\n\tGrade C"); }
else if(marks>=50 && marks<60)
{ printf("\n\tGrade D"); }
else
{ printf("\n\tFAIL!"); }
getch();
}
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
INPUT:
OUTPUT:
Date Sign.
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
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,
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]);
mark[0]);
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
Student’s tasks:
#include <stdio.h>
void main()
{
int arr[10],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");
}
OUTPUT:
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
#include <stdio.h>
int main() OUTPUT:
{
int marks[10], i, n, sum = 0, average;
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;
}
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();
}
OUTPUT:
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
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++)
OUTPUT:
/* 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();
}
#include<stdio.h> OUTPUT:
void main(void)
{ int a,b,c; char n[1000]; clrscr();
printf("\nENTER THE LIMIT OF VALUES
IN ARRAY YOU WANT TO INPUT:\t");
scanf("%d",&a);
for(b=0;b<a;b++)
{ printf("\nENTER THE %d VALUE:\t",b+1);
scanf("%s",&n[b]); }
printf("\nYOU ENTERED VALUES IN REVERSE :");
for(c=a;c>=0;c--)
{printf("\n\t%c",n[c]); }
getch();
}
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
6. Write a program that takes input for array int a[5] and array int b[5] and exchanges
their values.
#include<stdio.h>
void main(void)
{ int a[5],b[5],t,i,o,f,g,k; clrscr();
printf("\nEnter values of first array:\n");
for(i=0;i<5;i++)
{scanf("\t%d",&a[i]);}
printf("\nEnter values of OUTPUT:
second array:\n");
for(o=0;o<5;o++)
{scanf("\t%d",&b[o]);}
printf("\n");
printf("\nFIRST ARRAY");
for(g=0;g<5;g++)
{printf("\t%d",a[g]);}
printf("\nSECOND ARRAY");
for(k=0;k<5;k++)
{printf("\t%d",b[k]);}
for(t=0;t<5;t++)
{
a[t] = a[t] + b[t];
b[t] = a[t] - b[t];
a[t] = a[t] - b[t]; }
printf("\n\nNOW VALUES OF FIRST ARRAY");
for(f=0;f<5;f++)
{printf("\t%d",a[f]);}
printf("\nNOW VALUES OF SECOND ARRAY");
for(f=0;f<5;f++)
{printf("\t%d",b[f]);}
getch();
}
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
7. Write a program that takes 10 integers as input and prints the largest integer and its
location in the array.
#include <stdio.h>
void main(void)
{
int i , n, d , atg , arr[100];
clrscr();
printf("\n\tEnter total number of elements(1 to 100):\n\t");
scanf("%d", &n);
printf("\n");
for(i = 0; i < n;i++) OUTPUT:
{
printf("\n\tEnter Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\n\tTHE ARRAY VALUES
YOU ENTERED IS :\n");
for(d=0;d<n;d++)
{
printf("\t%d",arr[d]);
}
printf("\n");
for(i = 1; i < n; ++i)
{
}
printf("\n\tLargest element = %d", arr[0]);
getch();
}
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
8. Write a program to copy the contents of one array into another in the reverse order.
#include<stdio.h>
void main(void)
{
int a[10],b[10],i,j,d;
clrscr();
printf("\n\tEnter Elements :\n"); OUTPUT:
for(i=0;i<10;i++)
{ printf("\t");
scanf("%d",&a[i]);
}
printf("\n\n\tVALUES OF
ARRAY BEFORE REVERSE:\n\n\t");
for(d=0;d<10;d++)
{
printf("%d ",a[d]);
}
for(i=0,j=9;i<10;i++,j--)
{
b[i]=a[j];
}
printf("\n\n\tVALUES OF ARRAY
AFTER REVERSE \n\tCOPYING
IN ANOTHER ARRAY:\n\n\t");
for(i=0;i<10;i++)
{
printf("%d ",b[i]);
}
getch();
}
PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF
9. Write a program to find the transpose of a matrix (order of matrix given by user).
#include <stdio.h>
void main(void)
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]); OUTPUT:
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}
getch();
}
PROGRAMMING FUNDAMENTALS