PWC Practical 6
PWC Practical 6
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 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]);