Q9 Write A Program To Take User Name, Id, Department, 5 Subjects Marks and Make A Marksheet of It

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10
At a glance
Powered by AI
The document discusses arrays in C programming language. Arrays allow storing multiple values of the same data type in contiguous memory locations. An array is declared by specifying the data type, name and size. Arrays can be initialized during declaration by providing initial values inside curly braces. Elements of an array can be input and output using for loops along with array indexing.

An array is a collection of similar data types stored in contiguous memory locations. In C, an array is declared by specifying the data type, name of the array and size inside square brackets. The size must be a constant positive integer. The elements of an array are accessed using an index that ranges from 0 to size-1.

Arrays in C can be initialized during declaration by providing initial values inside curly braces. For example, int marks[5] = {34,45,67,89,90}; would initialize the first 5 elements of the array marks with the given values. If fewer initializers are provided than the array size, the remaining elements are initialized to 0.

CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF

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.

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]);

PROGRAMMING FUNDAMENTALS
CSC-19F-069 MUHAMMAD AZHAR Course Supervisor: SYEDA NAZIA ASHRAF

Student’s tasks:

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

#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

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

#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();
}

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


order

#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)
{

// Change < to > if you want to find the smallest element


if(arr[0] < arr[i])
{
arr[0] = arr[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

You might also like