0% found this document useful (0 votes)
4 views

PWC Practical 6

The document contains four programming tasks in C, each with code examples. Task A finds the largest element in an array, Task B computes the sum of array elements using pointers, Task C sorts an array in both ascending and descending order, and Task D collects personal information and marks from students to calculate their percentage and grade. Each task includes user input prompts and outputs the results in a formatted manner.

Uploaded by

omsmalik215
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 (0 votes)
4 views

PWC Practical 6

The document contains four programming tasks in C, each with code examples. Task A finds the largest element in an array, Task B computes the sum of array elements using pointers, Task C sorts an array in both ascending and descending order, and Task D collects personal information and marks from students to calculate their percentage and grade. Each task includes user input prompts and outputs the results in a formatted manner.

Uploaded by

omsmalik215
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/ 9

PRACTICAL 6

A] Write a program to find the largest element that is stored in an array.


Code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n, i, max;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
for (i = 0; i < n; i++)
{
printf("\nEnter element %d :",i+1);
scanf("%d", &arr[i]);
}
max = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
printf("The largest element in the array is: %d\n", max);
}
Output:

B] Write a program using pointer to compute the sum of all elements stored in array.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, sum = 0;
int *ptr;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
for (i = 0; i < n; i++)
{
printf("\nEnter element %d :",i+1);
scanf("%d", &arr[i]);
}
ptr = arr;
for (i = 0; i < n; i++)
{
sum += *(ptr + i);
}
printf("The sum of all elements in the array is: %d\n", sum);
}
Output:

C] Write a program to arrange the 'n' numbers stored in the array in ascending and
descending order.
Code:
#include <stdio.h>
#include<stdlib.h>

void sortAscending(int arr[], int n)


{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void sortDescending(int arr[], int n)


{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void printArray(int arr[], int n)


{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n\n");
}

void main()
{
int n, i;
printf("Name: Om Malik Roll No. FCS2425052\n");
printf("Enter size of array: ");
scanf("%d", &n);
int arr[n];
for(i = 0; i < n; i++)
{
printf("Enter Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Original array: ");
printArray(arr, n);

sortAscending(arr, n);
printf("Sorted in Ascending Order: ");
printArray(arr, n);

sortDescending(arr, n);
printf("Sorted in Descending Order: ");
printArray(arr, n);
}
Output:

D] Write a program to take user personal information and marks of three subjects from 5
students. Store their information in array variable. Calculate percentage and grade. Display
Complete result of each students with all the details in proper format.
Code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n = 5;
char names[5][50];
int ages[5];
int marks[5][3];
float percentages[5];
char grades[5];
printf("Name: Om Malik Roll no : FCS2425052 \n");
for (int i = 0; i < n; i++)
{
printf("Enter details for student %d\n", i + 1);
printf("Name: ");
scanf("%s", names[i]);
printf("Age: ");
scanf("%d", &ages[i]);

for (int j = 0; j < 3; j++)


{
printf("Marks in subject %d: ", j + 1);
scanf("%d", &marks[i][j]);
}
int totalMarks = 0;
for (int j = 0; j < 3; j++)
{
totalMarks += marks[i][j];
}
percentages[i] = (float)totalMarks / 3;

if (percentages[i] >= 90)


{
grades[i] = 'A';
}
else if (percentages[i] >= 80)
{
grades[i] = 'B';
}
else if (percentages[i] >= 70)
{
grades[i] = 'C';
}
else if (percentages[i]>=60)
{
grades[i] = 'D';
}
else if(percentages[i]>=50)
{
grades[i]='E';
}
else
{
grades[i]='F';
}
}
printf("\nComplete Results of Students:\n");
printf("-------------------------------------------------------------------------\n");
printf("Name\t\tAge\tMarks 1\tMarks 2\tMarks 3\tPercentage\tGrade\n");
printf("-------------------------------------------------------------------------\n");

for (int i = 0; i < n; i++)


{
printf("%s\t\t%d\t%d\t%d\t%d\t%.2f\t\t%c\n", names[i], ages[i],
marks[i][0], marks[i][1], marks[i][2],
percentages[i], grades[i]);
}
}
Output:

You might also like