The University of Lahore: Assignment
The University of Lahore: Assignment
Assignment:
“Computer Programming _ Lab”
Submitted To:
Sir Waseem
Submitted By:
Muhammad Sharoz Khalid
Roll No:
BSEE-01153076
Section:
C
Grade: ______________
Checked By:_________________
Date & Time:____________________
#include<iostream>
using namespace std;
int main()
{
int mat_1[3][3], mat_2[3][3], mat_3[3][3];
cout << "\t \t Enter the data in 1st matrix:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> mat_1[i][j];
}
}
cout << "\t \t Enter the data in 2nd matrix:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> mat_2[i][j];
}
}
cout << "\tMatrix A: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << mat_1[i][j] << " ";
}
cout << endl;
}
cout << "\tMatrix B: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << mat_2[i][j] << " ";
}
cout << endl;
}
//multiplication
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mat_3[i][j] = 0;
for (int m = 0; m < 3; m++)
{
mat_3[i][j] = mat_3[i][j] + mat_1[i][m] * mat_2[m][j];
}
}
}
//display result
cout << "\t\tThe Product of Matrix A and B: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << mat_3[i][j] << " " ;
}
cout << endl;
}
system ("pause");
return 0;
}
Program#2:
A program that reads a 2-d array as 3*3 matrix then checks whether the matrix is symmetric or
not. A matrix is symmetric if mat [ i ] [ j ] = mat [ j ] [ i ] for all i and j except when i = j.
3 1 12
1 9 10
12 10 7
Matrix is symmetric
#include<iostream>
using namespace std;
int main()
{
int mat_1[3][3];
cout << "\t \t Enter the data in 1st matrix:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> mat_1[i][j];
}
}
#include<iostream>
using namespace std;
Program#2:
Write a program that takes a string as argument and a character from user then passes both of them
to a function as arguments. If the character passed to the function is ‘g’ then it replaces all of the
‘g’ with ‘G’ and ‘G’ with ‘g’.
#include <iostream>
#include <string>
using namespace std;
void line(char abc[]);
int main()
{
char str[100];
cout << "Enter the string: ";
cin.getline(str, 100);
line(str);
system("pause");
return 0;
}
void line(char abc[])
{
int i;
char len, _char, c;
cout << "Enter the character to convert its case: ";
cin >> _char;
len = strlen(abc);
for (i = 0; i<len; i++)
{
if (toupper(abc[i]) == _char || tolower(abc[i]) == _char)
{
c = abc[i];
if (c >= 65 && c <= 90)
c = c + 32;
else if (c >= 97 && c <= 122)
c = c - 32;
abc[i] = c;
}
}
cout << abc << endl;;
}
Output
Enter a string
There are two tragedies in life. One is to lose your heart's desire. The other is to gain it.Enter a character
to replace: T
there are Two Tragedies in life. One is to lose your hearT's desire. the oTher is To gain iT.
Program#1:
Write a program that inputs two floating point numbers from user using pointer notation. Add two
times first value into second value. Add second value three times into first value. Then display the
final values stored in variables.
#include <iostream>
using namespace std;
int main()
{
float *ptr_1, *ptr_2;
float num_1, num_2;
ptr_1 = &num_1;
ptr_2 = &num_2;
cout << "Enter the first number: ";
cin >> *ptr_1;
cout << "Enter the second number: ";
cin >> *ptr_2;
cout << "The first number is: " << *ptr_1 << endl;
cout << "The second number is: " << *ptr_2 << endl;
cout << "After calculations: " << endl;
cout << "The first number becomes: " << (*ptr_1 + *ptr_2 * 2) << endl;
cout << "The second number becomes: " << (*ptr_2 + *ptr_1 * 3) << endl;
system("pause");
return 0;
}
Output
Program#2:
Write a program that inputs two integers and then swaps the values and the program finally
displays the value using pointers.
Enter first number: 13
Enter second number: 61
After swapping
First number is: 61
Second number is: 13
#include<iostream>
using namespace std;
int main()
{
float num_1, num_2;
float *ptr_1, *ptr_2;
cout << "enter num 1 = ";
ptr_1 = &num_1;
cin >> *ptr_1;
cout << "enter num 2 = ";
ptr_2 = &num_2;
cin >> *ptr_2;
cout << "num_1 = " << num_1 << "\nnum_2 = " << num_2 << endl;
cout << "Values After Swapping: " << endl;
float temp;
temp = *ptr_2;
*ptr_2 = *ptr_1;
*ptr_1 = temp;
cout << "num_1 = " << num_1 << "\nnum_2 = " << num_2 << endl;
system("pause");
return 0;
}
Program#3:
Write a program that inputs two characters from user and change their cases. If both characters are
small alphabets then convert them into capital alphabets, if both are capital alphabets then convert
them in small alphabets, if anyone of them is small and other is capital then change their cases.
#include <iostream>
using namespace std;
int main()
{
char char_1, char_2;
cout << "Enter an alphabet: ";
cin >> char_1;
cout << "Enter an alphabet: ";
cin >> char_2;
if (char_1 >= 65 && char_1 <= 90)
{
char_1 = char_1 + 32;
}
else
{
char_1 = char_1 - 32;
}
Output
Enter an alphabet: h
Enter an alphabet: Y
After changing case output is H and y
Program#4:
Write a program that inputs three numbers from user and then prints the medium value among the
three inputs.
#include <iostream>
using namespace std;
void medium(int, int, int);
int main()
{
int num_1, num_2, num_3;
cout << "Enter first number: ";
cin >> num_1;
cout << "Enter second number: ";
cin >> num_2;
cout << "Enter third number: ";
cin >> num_3;
else
{
if (b<c)
{
cout << "The number with medium value is = " << b << endl;
}
else if (c<a)
{
cout << "The number with medium value is = " << a << endl;
}
else
{
cout << "The number with medium value is = " << c << endl;
}
}
}
Output
Enter first number: 67
Enter second number: 17
Enter third number: 34
The number with medium value is 34
Program#5:
Write a program to input a number from keyboard until user enters a zero. Every time a number is
entered the program should display whether the number is greater than, less than, or equal to the
previous number.
#include <iostream>
using namespace std;
int main()
{
int num_1, num_2, num_3;
cout << "Enter a number: ";
cin >> num_1;
cout << "Enter a number: ";
cin >> num_2;
while (num_2 != 0)
{
if (num_2>num_1)
cout << num_2 << " is greater than " << num_1 << endl;
else if (num_2<num_1)
cout << num_2 << " is smaller than " << num_1 << endl;
else if (num_2 = num_1)
cout << num_2 << " is equal to " << num_1 << endl;
num_1 = num_2;
cout << "Enter a number: ";
cin >> num_2;
}
system("pause");
return 0;
}
Output
Enter a number: 26
Enter a number: 17
17 is smaller than 26
Enter a number: 32
32 is greater than 17
Enter a number: 32
32 is equal to 32
Enter a number: 0
Program#1:
Write a program that inputs an integer value from user using pointer notation. Pass that value to a
function named Check that receives value in a pointer. The function then checks if the number is
even it returns a pointer that stores square of the number, and returns cube otherwise. Display the
result in main.
#include <iostream>
using namespace std;
int Even_sqr(int *x);
int main()
{
int interger_value, *ptr;
ptr = &interger_value;
system("pause");
return 0;
}
int Even_sqr(int *x)
{
if (*x % 2 == 0)
return 0;
else
return 1;
}
Output
Enter a number: 13
Final result is: 2197
Program#2:
Write a program that inputs two integer values from user using pointer notation. Pass these values
to a function using pointers. The function then returns a pointer stored first – second if first value
is greater than second and returns first + second otherwise. Display the result in main.
#include <iostream>
using namespace std;
int Check(int *x, int *y);
int main()
{
int num_1, num_2, *ptr1, *ptr2;
ptr1 = &num_1;
ptr2 = &num_2;
Output
Enter first number: 13
Enter second number: 35
Final result is: 48
Program#3:
Write a program that inputs a string from user in main. Pass that string to a function using pointer.
The function then reverses the string using pointer notation and a single repetition statement. Also
display final string in main.
#include <iostream>
#include <string>
void stringReverse(const char[]);
using namespace std;
int main()
{
char strArray[50];
cin.getline(strArray,50);
int a = strlen(strArray);
stringReverse(strArray);
cout << endl;
system("pause");
return 0;
}
void stringReverse(const char strArray[])
{
if (strArray[0] == '\0')
return;
stringReverse(&strArray[1]);
cout << strArray[0];
}
Output
Enter a string
Arrays are called constant pointers
Reverse of string is
sretniop tnatsnoc dellac era syarrA
Program#4:
Write a program that inputs two integer arrays of size 8 from user using pointer notation. Pass
these arrays to a function using pointers. The function then returns 1 to main if both arrays are
reverse of each other and returns 0 otherwise. Display message in main accordingly.
#include <iostream>
using namespace std;
int Check_Reverse(int ary1[], int ary2[])
{
for (int i = 0; i < 8; i++)
{
cout << "Enter " << i << " index value of 1st array: ";
cin >> ary1[i];
cout << "Enter " << i << " index value of 2nd array: ";
cin >> ary2[i];
}
cout << "Enter Data in Arrays are as:" << endl;
for (int i = 0; i < 8; i++)
{
cout << i << " index value of 1st array: ";
cout << ary1[i] << endl;
}
for (int i = 0; i < 8; i++)
{
cout << i << " index value of 2nd array: ";
cout << ary2[i] << endl;
}
int check = 0;
for (int i = 0; i < 8; i++)
{
if (ary1[i] != ary2[7 - i])
check++;
}
if (check == 0)
return 1;
else
return 0;
}
int main()
{
int array_1[8], array_2[8];
int *array1_point = &array_1[8];
int *array2_point = &array_2[8];
int result = Check_Reverse(array1_point, array2_point);
if (result == 0)
{
cout << "Arrays are not reverse of each other." << endl;
}
else
cout << "Arrays are reverse of each other." << endl;
system("pause");
return 0;
}
Output
Enter values in first array: 7 5 19 28 16 10 18 39
Enter values in second array: 39 18 10 16 28 19 5 7
Both arrays are reverse of each other
Program#5:
Write a program that inputs a character array of size 10 from user. Pass that array to a function
using pointer that checks whether the characters in array are entered in ascending order that is the
first letter is smaller than the next one and so on. The function returns 0 to main if array is in
ascending order, returns 1 if array is in descending order and 2 otherwise. Print appropriate
messages in main.
#include <iostream>
using namespace std;
int Check_Order(char *ary1)
{
int assending=0,desending=0;
cout << "Enter Data in Arrays are as:" << endl;
for (int i = 0; i < 10; i++)
{
cout << ary1[i] << " ";
}
for (int i = 0; i<9; i++)
{
if (ary1[i]>ary1[i + 1])
{
desending ++;
continue;
}
else
break;
}
for (int i = 0; i < 9; i++)
{
if (ary1[i] < ary1[i + 1])
{
assending++;
continue;
}
else
break;
}
if (assending == 9)
return 0;
else if (desending == 9)
return 1;
else
return 2;
}
int main()
{
char array_1[10];
for (int i = 0; i < 10; i++)
{
cout << "Enter " << i << " index value of 1st array: ";
cin >> array_1[i];
}
int result = Check_Order(array_1);
cout << endl;
if (result == 0)
cout << "array is in ascending order."<< endl;
else if (result == 1)
cout << "array is in descending order." << endl;
else
cout << "array is not in order." << endl;
system("pause");
return 0;
}
Output
Enter characters in array: b e h i k m n q s y
Array is in Ascending order