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

Solution CC-112L PF Lab06Task List

The document contains a lab assignment on programming fundamentals in C. It includes 5 tasks covering topics like initializing and setting array elements, calculating sums of arrays, finding unique elements in an array, reversing an array, and more. For each task, it provides the problem statement, sample code solutions, and estimated time to complete. The tasks involve basic arrays, loops, functions, taking user input, and other core C programming concepts.

Uploaded by

Ahmad Butt
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)
112 views7 pages

Solution CC-112L PF Lab06Task List

The document contains a lab assignment on programming fundamentals in C. It includes 5 tasks covering topics like initializing and setting array elements, calculating sums of arrays, finding unique elements in an array, reversing an array, and more. For each task, it provides the problem statement, sample code solutions, and estimated time to complete. The tasks involve basic arrays, loops, functions, taking user input, and other core C programming concepts.

Uploaded by

Ahmad Butt
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
You are on page 1/ 7

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]={0}; //initialize an array of size 5, here all values
// in array is set to 0.
for(int i=0;i<5;i++)
arr[i]=i; //set each elements of array corresponding to their index

for(int i=0;i<5;i++)
printf("Value at index %d is : %d\n",i,arr[i]); //display the array
return 0;
}

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 s=0;
int arr[5]={1,2,3,4,5};//initialize array using "initializer list"
for(int i=0; i<5; i++)
s = s + arr[i];

printf("Sum : %d", s);

return 0;
}
Prof. Usman Ahmad. Dept. of IT & CS Page |1 Govt. Islamia Graduate College, Gujranwala.
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];
int unique=0,num=0;
printf("Enter 5 numbers in the Array : \n");
for(int i=0;i<5;i++)
{
printf("Enter number at index %d :",i);
scanf("%d",&arr[i]);
}
printf("Unique numbers are : \n");
for (int i = 0; i < 5; i++) {
unique = 0;
for (int j = 0; j < 5; j++) {
if (i != j) {
if (arr[i] == arr[j]) {
unique = 1;
break;
}
}
}
if (unique == 0) {
num++;
printf("%d, ", arr[i]);
}
}
if (num > 0)
printf("\b\b ");
else
printf("There are no Unique numbers.");

return 0;
}

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

Prof. Usman Ahmad. Dept. of IT & CS Page |2 Govt. Islamia Graduate College, Gujranwala.
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];
int unique=0,num=0;
printf("Enter 5 numbers in the Array : \n");
for(int i=0;i<5;i++)
{
printf("Enter number at index %d :",i);
scanf("%d",&arr[i]);
}
//use any: solution 1 or solution 2
Solution 1 Solution 2
int start=0,end=5-1,temp; for (int j = 0; j <= 4-j; j++) {
while (start < end) temp = arr[j];
{ arr[j] = arr[4 - j];
temp = arr[start]; arr[4 - j] = temp;
arr[start] = arr[end]; }
arr[end] = temp;
start++;
end--;
}

printf("\nStudent Roll numbers in array after Reverse:\n");


for(int i=0;i<5;i++)
printf("%d ",arr[i]);
return 0;
}

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


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

Prof. Usman Ahmad. Dept. of IT & CS Page |3 Govt. Islamia Graduate College, Gujranwala.
Solution:
#include <stdio.h>
void printArray(int[]);
int main() {
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
printArray(arr);
}
void printArray(int arr[]) {
for (int i = 0; i < 10; i++)
printf("%d\t", arr[i]);
}

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


(a) Dry Run the following programs and fill up the trace table:

Solution:
Line No Output on Console
6 Element at index 0: 1 Element at index 1: 7
7 Element at index 0: 9 Element at index 1: 6
8 Element at index 0: 9 Element at index 1: 13
9 Element at index 0: 1 Element at index 1: 7
10 Element at index 0: 9 Element at index 1: 19

Prof. Usman Ahmad. Dept. of IT & CS Page |4 Govt. Islamia Graduate College, Gujranwala.
Task 05: Find and resolve the errors. [30 minutes / 20 marks]

(a):

Solution:
#include <stdio.h>
int main() {
int Array[] = { 1,2,3 };
Array[2] = 2;
char Arr[6] = "letter";
for (int i = 0; i < 3; i++)
printf("%d", Array[i]);
return 0;
}

(b):

Solution:
#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]);
}

Prof. Usman Ahmad. Dept. of IT & CS Page |5 Govt. Islamia Graduate College, Gujranwala.
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>

int main(){
int a[5],i;
float GP[5],GPA=0.0;

printf("Marks in Subjects:\n");
for(int i=0;i<5;i++)
scanf("%d",&a[i]);

for(int i=0;i<5;i++){
if(a[i]>=85)
GP[i]=4.0;
else if(a[i]>=80 && a[i]<85)
GP[i]=3.7;
else if(a[i]>=75 && a[i]<80)
GP[i]=3.3;
else if(a[i]>=70&&a[i]<75)
GP[i]=3.0;
else if(a[i]>=65&&a[i]<70)
GP[i]=2.7;
else if(a[i]>=61&&a[i]<65)
GP[i]=2.3;
else if(a[i]>=52&&a[i]<61)
GP[i]=2.0;
else
GP[i]=0.0;
}

for(int i=0;i<5;i++)
GPA=GP[i]+GPA;

GPA=GPA/5;
printf("\nGPA: %.2f",GPA);

return 0;
}

Prof. Usman Ahmad. Dept. of IT & CS Page |6 Govt. Islamia Graduate College, Gujranwala.
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>
int main()
{
char string[10], s;
int c = 0, count[26] = {0}, x, max = 0;

printf("Enter a string\n");
gets(string);

while (string[c] != '\0') {


/** Considering characters from 'a' to 'z' only and ignoring others. */

if (string[c] >= 'a' && string[c] <= 'z') {


x = string[c] - 'a';
count[x]++;
}
c++;
if (max < count[x])
max = count[x] ;

printf("\nHighest Frequency Letter: ");


for (c = 0; c < 26; c++)
if(count[c]==max)
printf("%c = %d ", c + 'a',count[c]);

return 0;
}

Prof. Usman Ahmad. Dept. of IT & CS Page |7 Govt. Islamia Graduate College, Gujranwala.

You might also like