Solution CC-112L PF Lab06Task List
Solution CC-112L PF Lab06Task List
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;
}
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];
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;
}
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--;
}
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]);
}
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);
return 0;
}
Prof. Usman Ahmad. Dept. of IT & CS Page |7 Govt. Islamia Graduate College, Gujranwala.