0% found this document useful (0 votes)
17 views

Question A

The document discusses various types of errors encountered while programming in C language and provides solutions to address them. It also includes sample code to demonstrate: 1) Swapping two arrays completely; 2) Swapping the even elements of one array with the odd elements of another; 3) Checking if the sum of even/odd elements of one array is greater than the other, and swapping the arrays if yes.

Uploaded by

Ghassaan Khalid
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)
17 views

Question A

The document discusses various types of errors encountered while programming in C language and provides solutions to address them. It also includes sample code to demonstrate: 1) Swapping two arrays completely; 2) Swapping the even elements of one array with the odd elements of another; 3) Checking if the sum of even/odd elements of one array is greater than the other, and swapping the arrays if yes.

Uploaded by

Ghassaan Khalid
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/ 40

Question A:

Write 20 types of errors you have faced during programming in C language. Also write the solution for
those who have been adressed correctly and successfully.

Answer:

The main types of errors faced by me while coding in C are as follows:

Syntax Error:

Syntax errors occurs when I do not follow the set of rules defined for the syntax of C language. The most
common syntax errors are:

i- Missing a semi-colon (;)


ii- Missing parenthesis ({})
iii- Assigning a value to a variable without declaring it.

Runtime Error:

Runtime errors can occur because of various reasons. Some of the reasons are:

i- Mistakes in the code (for example while execution of a loop, i forgot to enter the break statement for
the loop so it continued for infinite times)
ii- Mathematical (like dividing a number by zero)
iii- Undefined variables.

Logical Error:

Sometimes, we do not get the output we expected after the compilation and execution of a program.
For example when I mistakenly used single '=' in an if statement instead of '=='.

Semantic Error:

Sometimes when I code and compiler tells me that 'Code has no effect' but in my mind, I have written a
fine code. It usually happens when compiler doesn't understand the code though it doesn't have any
erros. For example, once i tried to add an integer into a string.

Linker Error:
While writing a program in C when I was new to programming, I capitalized the 'm' in method 'Main()'
instead of 'main()' and the program showed error. Later on I got to know that case sensitivity matters
alot in programming.

Question B:

Write a program in C having two arrays A and B and then swap complete array A and B.

Answer:

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

int a[3][3], b[3][3];

int row, col, c;

clrscr();

for(row=0; row<3; row++)

for(col=0; col<3; col++)

{
printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);

printf("\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);

scanf("%d", &b[row][col]);

printf("\n");

printf("\n");

sumofa=a[0][1]+a[1][0]+a[1][2]+a[2][1];

sumofb=b[0][1]+b[1][0]+b[1][2]+b[2][1];

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)


{

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

if(sumofa>sumofb)

printf("\n\nAfter Swapping\n\n");

for(row=0; row<3; row++)


{

for(col=0; col<3; col++)

c=a[row][col];

a[row][col]=b[row][col];

b[row][col]=c;

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)


{

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

getch();

Question C:

Swap all evens of array A with odds of B.

Answer:

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

{
int a[3][3], b[3][3];

int row, col, c, e;

clrscr();

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);

printf("\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);

scanf("%d", &b[row][col]);

}
printf("\n");

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

}
printf("\n");

printf("\n\nAfter Swapping A's even placeholders with B's odd placeholders\n\n");

for(row=0; row<=3; row+=2)

for(col=1; col<=3; col+=2)

c=a[row][col];

a[row][col]=b[row][col-1];

b[row][col-1]=c;

e=a[1][2];

a[1][2]=b[1][2];

b[1][2]=e;

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)


{

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

getch();

}
Question D:

Swap all odds of array A with evens of B.

Solution:

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

int a[3][3], b[3][3];

int row, col, c, e;

clrscr();

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);

printf("\n");
}

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);

scanf("%d", &b[row][col]);

printf("\n");

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

}
printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

printf("\n\nAfter Swapping A's odd placeholders with B's even placeholders\n\n");

for(row=0; row<=3; row+=2)

for(col=0; col<=3; col+=2)

c=a[row][col];

a[row][col]=b[row][col+1];

b[row][col+1]=c;

e=a[1][2];
a[1][2]=b[1][2];

b[1][2]=e;

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

}
printf("\n");

getch();

Question E:

Check if sum of evens of A is greater then sum of evens B then swap.

Solution:

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

int a[3][3], b[3][3];

int row, col, c, sumofa=0, sumofb=0;

clrscr();
for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);

printf("\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);

scanf("%d", &b[row][col]);

printf("\n");

printf("\n");

sumofa=a[0][1]+a[1][0]+a[1][2]+a[2][1];

sumofb=b[0][1]+b[1][0]+b[1][2]+b[2][1];
printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

}
if(sumofa>sumofb)

printf("\n\nAfter Swapping\n\n");

for(row=0; row<3; row++)

for(col=1; col<3; col+=2)

c=a[row][col];

a[row][col]=b[row][col];

b[row][col]=c;

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");
}

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

getch();

Question F:

Check if sum of odds of A is greater then odds of B then swap.

Solution:

Source Code:
#include<stdio.h>

#include<conio.h>

void main()

int a[3][3], b[3][3];

int row, col, c, sumofa, sumofb;

clrscr();

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);

printf("\n");

for(row=0; row<3; row++)

{
for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);

scanf("%d", &b[row][col]);

printf("\n");

printf("\n");

sumofa=a[0][0]+a[0][2]+a[1][1]+a[2][0]+a[2][2];

sumofb=b[0][0]+b[0][2]+b[1][1]+b[2][0]+b[2][2];

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");
for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

if(sumofa>sumofb)

printf("\n\nAfter Swapping All Odds\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col+=2)

c=a[row][col];

a[row][col]=b[row][col];

b[row][col]=c;

}
printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

}
}

getch();

Question G:

If first value of A is greater then B then swap otherwise do not swap.

Solution:

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

int a[3][3], b[3][3];

int row, col, c;

clrscr();
for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);

printf("\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);

scanf("%d", &b[row][col]);

printf("\n");

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)


{

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

if(a[0][0]>b[2][2])

{
printf("\n\nAfter Swapping\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

c=a[row][col];

a[row][col]=b[row][col];

b[row][col]=c;

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");
for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

getch();

Question H:

If sum of first and last values of A is greater than the sum of first and last values of B swap, otherwise do
not.

Solution:

Source Code:

#include<stdio.h>

#include<conio.h>
void main()

int a[3][3], b[3][3];

int row, col, c;

clrscr();

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);

printf("\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);


scanf("%d", &b[row][col]);

printf("\n");

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

{
printf("\t%d", b[row][col]);

printf("\n");

if(a[0][0]+a[2][2]>b[0][0]+[2][2])

printf("\n\nAfter Swapping\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

c=a[row][col];

a[row][col]=b[row][col];

b[row][col]=c;

printf("\n");

printf("Matrix A\n\n");

for(row=0; row<3; row++)


{

for(col=0; col<3; col++)

printf("\t%d", a[row][col]);

printf("\n");

printf("\nMatrix B\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%d", b[row][col]);

printf("\n");

getch();
}

Question I:

Sort all values of a matrix in ascending order.

Solution:

Source Code:

#include <stdio.h>
#include <conio.h>

void main()

clrscr();

int a[3][3];

int row, col, c;

printf("Enter the elements of the matrix:\n");

for (row = 0; row < 3; row++)

for (col = 0; col < 3; col++)

printf("Enter value no. %d of row %d: ", col+1, row+1);

scanf("%d", &a[row][col]);

for (int k = 0; k < 9; k++) {


for (row = 0; row < 3; row++) {

for (col = 0; col < 2; col++) {

if (a[row][col] > a[row][col + 1]) {

c = a[row][col];

a[row][col] = a[row][col +1];

a[row][col+1] = c;

printf("\nMatrix in ascending order:\n");

for ( row = 0; row < 3; row++)

for (col = 0; col < 3; col++)

printf("\t%d ", a[row][col]);

printf("\n");

getch();

}
Question J:

Sort all values of matrix in descending order.

Solution:

Source Code:

#include <stdio.h>
#include <conio.h>

void main()

clrscr();

int a[3][3];

int row, col, c;

printf("Enter the elements of the matrix:\n");

for (row = 0; row < 3; row++)

for (col = 0; col < 3; col++)

printf("Enter value no. %d of row %d: ", col+1, row+1);

scanf("%d", &a[row][col]);

for (int k = 0; k < 9; k++) {

for (row = 0; row < 3; row++) {

for (int col = 0; col < 2; col++) {

if (a[row][col] < a[row][col + 1]) {


c = a[row][col];

a[row][col] = a[row][col +1];

a[row][col+1] = c;

printf("\nMatrix in descending order:\n");

for ( row = 0; row < 3; row++)

for (col = 0; col < 3; col++)

printf("\t%d ", a[row][col]);

printf("\n");

getch();

Question K:

If there are A and B matrix, display matrix C as:

* Multiplication of values if similar indexed values are even


* Addition if similar indexed values are odd

* Divide if similar A's value is greater than B's value

* Subtract if both values are equal

Solution:

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

int a[3][3], b[3][3];

float c[3][3];

int row, col;

clrscr();

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array A: ", col+1, row+1);

scanf("%d", &a[row][col]);
}

printf("\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("Enter no. %d value for row %d of array B: ", col+1, row+1);

scanf("%d", &b[row][col]);

printf("\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

if(a[row][col]%2==0 && b[row][col]%2==0 && a[row][col]!=b[row][col] &&


a[row][col]<<b[row][col])

c[row][col]=a[row][col]*b[row][col];

else if(a[row][col]%2!=0 && b[row][col]%2!=0 && a[row][col]!=b[row][col])

{
c[row][col]=a[row][col]+b[row][col];

else if(a[row][col]>b[row][col])

c[row][col]=(float)a[row][col]/b[row][col];

else if(a[row][col]==b[row][col])

c[row][col]=a[row][col]-b[row][col];

printf("\n");

printf("Matrix C\n\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("\t%.1f", c[row][col]);
}

printf("\n\n");

getch();

You might also like