0% found this document useful (0 votes)
29 views7 pages

C Programming: Array Operations Lab

The document contains details about programming tasks related to arrays in C language. It includes tasks to initialize and access array elements, calculate sum of elements, find unique elements, reverse an array, and more. Solutions to the tasks are also provided in C code.

Uploaded by

Hashim Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

C Programming: Array Operations Lab

The document contains details about programming tasks related to arrays in C language. It includes tasks to initialize and access array elements, calculate sum of elements, find unique elements, reverse an array, and more. Solutions to the tasks are also provided in C code.

Uploaded by

Hashim Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CC-112L Programming Fundamentals Lab 06

Task 01: Set Element Values [Estimated 15 minutes / 20 marks]


Create a C program which:
Initialize an integer array of size 5
Set elements of arrays corresponding to their index (subscript) value using loop statement
Display the array elements on Console
The output should be as follows:

Solution
#include <stdio.h>
int main() {
int arr[5] = { 1,2,3,4,5 };
for (int i = 0; i < 5; i++) {
printf("Value at index %d is: %d\n", i, arr[i]);
}
}

Task 02: Sum of Elements [Estimated 10 minutes / 15 marks]


Create a C program which:
Initialize an integer array of size 5 with “initializer list”
Calculate sum of the elements of array loop statement
Display the Sum on Console

Solution
#include <stdio.h>

int main() {

int arr[5] = { 1,2,3,4,5 };

for (int i = 0; i < 5; i++) {

printf("Value at index %d is: %d\n", i, arr[i]);

Task 01: Unique} Elements [40 minutes / 30 marks]


Write a C program which:
Defines an array of size “5”
Set Values of array by taking input from user
Displays the unique elements on the Console
If no element is unique then displays the message “No element is unique”
Input Output
Unique Elements: 2, 3, 4, 5,
23456
6
89 89 55 34 55 Unique Element: 34
33 33 33 33 33 No element is unique

Solution
#include <stdio.h>
int main() {
int arr[5], uniq, num=0;
for (int i = 0; i < 5; i++) {
printf("Enter a number : ");
scanf("%d", &arr[i]);
}
printf("Unique elements are : ");
for (int i = 0; i < 5; i++) {
uniq = 0;
for (int j = 0; j < 5; j++) {
if (i != j) {
if (arr[i] == arr[j]) {
uniq = 1;
break;
}
}
}if (uniq == 0) {
num++;
printf("%d, ", arr[i]);
}
}
if (num==0)
printf("\rNo element is uniq");
}

Task 02: Reverse an Array [30 minutes / 25 marks]


Write a C program which:
Defines an Array of size “5”
Initialize the array by taking input from user
Reverse the elements of array i.e., element at first index should be on the last index and vice versa
Output (After Reversing
Input
Array)
23456 65432
9 13 12 5 99 99 5 12 13 9
Solution
#include <stdio.h>
int main() {
int arr[5], temp = 0;
for (int i = 0; i < 5; i++) {
printf("Enter a number : ");
scanf("%d", &arr[i]);
}printf("before reverse :");
for (int i = 0; i < 5; i++) {
printf("%d\t",arr[i]);}
for (int j = 0; j <= 4-j; j++) {
temp = arr[j];
arr[j] = arr[4 - j];
arr[4 - j] = temp;
}
printf("\n\nAfter reverse : ");
for (int k = 0; k < 5; k++) {
printf("%d\t", arr[k]);
}
}

Task 03: Fill the blank area [20 minutes / 20 marks]


The following program:
Defines an array
Print the elements by calling a function

Task 04: Dry Run Programs. [30 minutes / 25 marks]


(a) Dry Run the following programs and fill up the trace table:
Solution
#include <stdio.h>
void printarr(int[]);
int main() {
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
printarr(arr);
Return 0;
}
void printarr(int arr[]) {
for (int i = 0; i < 10; i++) {
printf("%d\t", arr[i]);
}
}

Line No Output on Console


Element at index 0: 1 Element at
6 index 1: 7
Element at index 0: 9 Element at
7 index 1: 6
Element at index 0: 9 Element at
8 index 1: 12
Element at index 0: 1 Element at
9 index 1: 7
Element at index 0: 9 Element at
10 index 1: 18
Task 05: Find and resolve the errors. [30 minutes / 20 marks]
(a):
#include <stdio.h>
int main() {
int Array[] = { 1,2,3 };
Array[2] = 2;
char Arr[7] = {"letter"};
for (int i = 0; i < 6; i++) {
printf("%c", Arr[i]);
}
}

(b): #include <stdio.h>


void Array(int[], int);
int main(){
int Arr[] = {1,2,3};
Array(Arr, 3);
return 0;
}
void Array(int Array[], int size)
{
for(int i = 0; i < size; i++)
{
printf(“Element at index is %d: %d”, i, Array[i]);
}
}

Post-Lab Activities:
Task 01: GPA Calculation [Estimated 30 minutes / 30 marks]
Write a C program which:
Defines an array of size “5”
Set Values of array by taking Subject Marks as an input from user
Pass the array to the function
The function calculates & returns the GPA
Display the GPA on the Console
Input Output
85 88 90 85 99 GPA: 4.0
83 99 72 77 82 GPA: 3.54

Solution
#include <stdio.h>
float GPA(int[]);
int main() {
int marks[5];
for (int i = 0; i < 5; i++) {
printf("Enter Marks : ");
scanf("%d", &marks[i]);
}
printf("Total GPA = %.2f", GPA(marks));
}
float GPA(int m[]) {
int obt = 0;
float per;
for (int i = 0; i < 5; i++) {
obt += m[i];
}
per = (obt * 100) / 500;
if (per >= 85) return 4.00;
if (per >= 80 && per < 85) return 3.70;
if (per >= 75 && per < 80) return 3.30;
if (per >= 70 && per < 75) return 3.00;
if (per >= 65 && per < 70) return 2.70;
if (per >= 61 && per < 65) return 2.30;
if (per >= 58 && per < 61) return 2.00;
if (per >= 55 && per < 58) return 1.70;
if (per >= 50 && per < 55) return 1.00;
if (per >= 0 && per < 50) return 0.00;
}

Task 02: Frequency of letters


[Estimated 60 minutes / 30 marks]
Write a C program which:
Defines a character array of size “10”
Set Values of array by taking input from user
Find the letter with highest frequency and display on Console
Input Output
Abilities Highest Frequency Letter: I
Drizzling Highest Frequency Letter: I, Z
Solution

#include <stdio.h>
#include <ctype.h>
int main() {
char word[10];
int max = 0,fre[10]={0};
printf("Enter a Word (1 to 10 characters) : ");
gets(word);
for (int i = 0; i < 10; i++){
if (word[i] >= 'a' && word[i] <= 'z'||word[i] >= 'A' && word[i] <= 'Z')
{

for (int j = i; j < 10; j++) {

if (word[i] == word[j]) {
fre[i]++;
}
if (fre[i] > max) max = fre[i];
}

}}
printf("Most Frequent Character: ");
for (int i = 0; i < 10; i++) {
if (fre[i] == max) {
printf("%c, ", toupper(word[i]));
}
}
}

You might also like