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

The University of Lahore: Assignment

The document contains 4 programs written in C++ to solve various problems: 1. A program that reads two 3x3 matrices as 2D arrays, calculates their product, and stores it in a third 2D array. 2. A program that reads a 3x3 matrix and checks if it is symmetric. 3. Two programs - one shifts negative numbers in an integer array to the left and positive to the right. The other replaces all 'g' with 'G' and vice versa in a string. 4. Two programs - one performs calculations on two numbers using pointers. The other swaps the values of two integers using pointers.

Uploaded by

Waqar Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

The University of Lahore: Assignment

The document contains 4 programs written in C++ to solve various problems: 1. A program that reads two 3x3 matrices as 2D arrays, calculates their product, and stores it in a third 2D array. 2. A program that reads a 3x3 matrix and checks if it is symmetric. 3. Two programs - one shifts negative numbers in an integer array to the left and positive to the right. The other replaces all 'g' with 'G' and vice versa in a string. 4. Two programs - one performs calculations on two numbers using pointers. The other swaps the values of two integers using pointers.

Uploaded by

Waqar Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

The University Of Lahore

Assignment:
“Computer Programming _ Lab”
Submitted To:
Sir Waseem
Submitted By:
Muhammad Sharoz Khalid

Roll No:
BSEE-01153076

Section:
C

Grade: ______________
Checked By:_________________
Date & Time:____________________

Department of Electrical Engineering


Program#1:
A program that reads two 2-d arrays as 3*3 matrices then calculates and stores their product into
another 2-d array of size 3*3.
Array A
3 5 6
1 9 2
12 10 7
Array B
4 6 7
2 3 5
3 8 6
Product of Array A and B is
30 81 82
28 49 64
89 158 176

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

cout << "\tMatrix : " << endl;


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << mat_1[i][j] << " ";
}
cout << endl;
}
int result=0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i != j)
{
result = (mat_1[i][j] - mat_1[j][i]);
}
}
}
if (result != 0)
{
cout << "The matrix is not symmetric." << endl;
}
else
cout << "The matrix is symmetric." << endl;
system("pause");
return 0;
}
Program#1:
Write a function that takes an integer array and its size as arguments and then shift the negative
numbers to the left and positive numbers to the right.
Output
Enter numbers in the array: 3 -5 1 3 7 0 -15 3 -7 -8
The array after function call is: -5 -15 -7 -8 3 1 3 7 0 3

#include<iostream>
using namespace std;

void L_to_R(int arrayyy[], int n)


{
for (int i = 0; i < n; i++)
{
if (arrayyy[i] < 0)
cout << arrayyy[i] << " ";
}
for (int i = 0; i < n; i++)
{
if (arrayyy[i] >= 0)
cout << arrayyy[i] << " ";
}
cout << endl;
}
int main()
{
int n;
cout << "Enter Size of Array = ";
cin >> n;
int arrayy[50];
cout << "Enter the Values in Array:" << endl;
for (int i = 0; i < n; i++)
{
cin >> arrayy[i];
}
cout << "Entered Array:" << endl;
for (int i = 0; i < n; i++)
{
cout << arrayy[i] <<" ";
}
cout << endl;
cout << "Sorted array as left (-) to Right(+) :" << endl;
L_to_R(arrayy, n);
system("pause");
return 0;
}

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;
}

if (char_2 >= 65 && char_2 <= 90)


{
char_2 = char_2 + 32;
}
else
{
char_2 = char_2 - 32;
}
cout << "After changing case output is " << char_1 << " and " << char_2 << endl;
system("pause");
return 0;
}

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;

medium(num_1, num_2, num_3);


system("pause");
return 0;
}

void medium(int a, int b, int c)


{
if (a>b)
{
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;
}
}

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;

cout << "Enter the number = ";


cin >> interger_value;
if (Even_sqr(ptr) == 0)
cout << "Final Result = " << interger_value * interger_value << endl;
else
cout << "Final Result = " << interger_value * interger_value *
interger_value << endl;

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;

cout << "Enter the first number: ";


cin >> num_1;
cout << "Enter the second number: ";
cin >> num_2;
int result = Check(ptr1, ptr2);
if (result == 0)
cout << "Final Result = " << num_1 - num_2 << endl;
else
cout << "Final Result = " << num_1 + num_2 << endl;
system("pause");
return 0;
}

int Check(int *x, int *y)


{
if (*x > *y)
return 0;
else
return 1;
}

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

You might also like