PPSC MID-II Unit-3 ASSIGNMENT QUESTIONS
PPSC MID-II Unit-3 ASSIGNMENT QUESTIONS
Mid-II
Unit-3 Assignment Questions
Ans:
Definition: An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection
of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each
data element can be randomly accessed by using its index number.
data_type array_name[array_size];
The simplest way to initialize a Single Dimensional array is by using the index of each
element. We can initialize each element of the array by using the index. Consider the following
example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Program:
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
Output:
80
60
70
85
75
Program:
#include <stdio.h>
int main()
{
int arr[100], n,ctr=0;
int i, j, k;
printf("Print all unique elements of an array:\n");
printf("------------------------------------------\n");
printf("Input the number of elements to be stored in the array: ");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr[i]);
}
printf("\nThe unique elements found in the array are: \n");
for(i=0; i<n; i++)
{
ctr=0;
for(j=0,k=n; j<k+1; j++)
{
/*Increment the counter when the seaarch value is duplicate.*/
if (i!=j)
{
if(arr[i]==arr[j])
{
ctr++;
}
}
}
if(ctr==0)
{
printf("%d ",arr[i]);
}
}
printf("\n\n");
return 0;
}
Expected Output:
Actual Output:
Print all unique elements of an array:
------------------------------------------
Input the number of elements to be stored in the array: 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 8
element - 2 : 1
element - 3 : 4
element - 4 : 6
The unique elements found in the array are:
28146
Ans: The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns. However, 2D arrays are
created to implement a relational database lookalike data structure. It provides ease of holding the
bulk of data at once which can be passed to any number of functions wherever required.
data_type array_name[rows][columns];
int twodimen[4][3];
In the 1D array, we don't need to specify the size of the array if the declaration and initialization
are being done simultaneously. However, this will not work with 2D arrays. We will have to
define at least the second dimension of the array. The two-dimensional array can be declared and
defined in the following way.
1. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Program:
#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}
Output:
arr[0] [0] = 1
arr[0] [1] = 2
arr[0] [2] = 3
arr[1] [0] = 2
arr[1] [1] = 3
arr[1] [2] = 4
arr[2] [0] = 3
arr[2] [1] = 4
arr[2] [2] = 5
arr[3] [0] = 4
arr[3] [1] = 5
arr[3] [2] = 6
4. Build a C program to find the sum & average of n numbers using array
Program:
#include<stdio.h>
int main()
{
int i,n,sum=0,num;
float avg;
printf("\nEnter How many Number you want?\n");
scanf("%d",&n);
printf("\nEnter elements one by one\n");
for(i=0;i<n;++i)
{
scanf("%d",&num);
sum = sum +num;
}
avg = sum/(float)n;
return 0;
}
Output:
5. Explain about string input & output functions with suitable examples.
Ans: The gets() and puts() are declared in the header file stdio.h, are functions are involved
in the input/output operations of the strings.
C gets() function
The gets() function enables the user to enter some characters followed by the enter key. All
the characters entered by the user get stored in a character array. The null character is added
to the array to make it a string. The gets() allows the user to enter the space-separated strings.
It returns the string entered by the user.
Declaration
char[] gets(char[]);
Example:
#include<stdio.h>
void main ()
{
char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s);
}
Output:
The gets() function is risky to use since it doesn't perform any array bound checking
and keep reading the characters until the new line (enter) is encountered. It suffers from
buffer overflow, which can be avoided by using fgets().
C puts() function
The puts() function is very much similar to printf() function. The puts() function is used to
print the string on the console which is previously read by using gets() or scanf() function.
The puts() function returns an integer value representing the number of characters being
printed on the console. Since, it prints an additional newline character with the string, which
moves the cursor to the new line on the console, the integer value returned by puts() will
always be equal to the number of characters present in the string plus 1.
Declaration
int puts(char[])
Example:
#include<stdio.h>
#include <string.h>
int main(){
char name[50];
printf("Enter your name: ");
gets(name); //reads string from user
printf("Your name is: ");
puts(name); //displays string
return 0;
}
Output:
Enter your name: Hari
Your name is: Hari
Program:
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf(“\nEnter the number of elements : “);
scanf(“%d”,&n);
printf(“\nInput the array elements : “);
for(i=0;i<n;++i)
scanf(“%d”,&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
return 0;
}
Output:
The strlen() function returns the length of the given string. It doesn't count null
character '\0'.
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}
Output:
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
}
Output:
#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'c', '\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
}
Output:
Here, we are using gets() function which reads string from the console.
#include<stdio.h>
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
Output:
The strrev(string) function returns reverse of the given string. Let's see a simple
example of strrev() function.
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
Output:
Program:
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, space;
return 0;
}
Output:
Syntax:
char variable_name[r] = {list of string};
Here,
var_name is the name of the variable in C.
r is the maximum number of string values that can be stored in a string array.
c is a maximum number of character values that can be stored in each string
array.
Example:
#include <stdio.h>
// Driver code
int main()
{
char arr[3][10] = {"Geek",
"Geeks", "Geekfor"};
printf("String array Elements are:\n");
Output:
Program:
#include<stdio.h>
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int m1[r1][c1],m2[r2][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;
Output:
11. Explain how to declare and initialize a three dimensional array with
suitable examples.
Ans: In C programming, you can create an array of arrays. These arrays are known
as multidimensional arrays. A 3D Array is an array which contains 3 dimensions.
The following is the syntax:
datatype arrayname[size1][size2][size3];
int test[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};
Example:
#include <stdio.h>
int main()
{
int test[2][3][2];
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
Output:
Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12
Displaying Values:
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
printf("\n");
Output:
Elements of original array:
52871
Elements of array sorted in ascending order:
12578
Ans:
#include<stdio.h>
void main()
int mat[12][12];
int i,j,row,col,temp;
scanf("%d%d",&row,&col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&mat[i][j]);
printf("The matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d\t",mat[i][j]);
printf("\n");
for(i=0;i<row;i++)
for(j=0;j<i;j++)
temp=mat[i][j];
mat[i][j]=mat[j][i];
mat[j][i]=temp;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d\t",mat[i][j]);
printf("\n");
Output:
Ans:
#include<stdio.h>
#include<string.h>
int rev() //Open rev function
{
char st[100];
int len,i;
gets(st); //Input the string
len=strlen(st); //Calculate length of the string using strlen() function
printf("\nThe reverse string is:");
for(i=len;i>=0;i--) //Loop to print reverse string
{
printf("%c",st[i]); //Print the reverse string's character one by one
}
};
int main() //Code given by Susobhan Akhuli
{
printf("Enter the string: ");
rev(); //Call rev function
return 0;
}
Output: