0% found this document useful (0 votes)
25 views25 pages

Document 7 1

Uploaded by

anaszahid626
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)
25 views25 pages

Document 7 1

Uploaded by

anaszahid626
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/ 25

Subject :Programming Fundamental

Name :Amaz Hafeez


Section: V13
I’D: F2024266603
Professor: Shahbaz Qadeer
Assignment No 3:
Question No 1:
Input:
int A [5][2], k, j;
for (k = 0; k <= 4; k++)
for (j=0; j<2; j++)
A[k][j] = k + j;
for (k = 4; k>= 0; k --2)
cout<<A[k] [0] <<A[k][1];
for (k = 4; k> 0; k -=2)
cout<<A[k-1][0] <<A[k-1][1];

Output:
4523013412
Question No 2:
Input:
int A [3] = 133, 21, 34);
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3;j++)
{
if (A[j] > A[i]) I
{
A[i] += All: All
A[i]- All;
A[i] = A[i] - AGE
}
}
}
for (i=0; i<3; i++)
cout << A[i] << " ";

Output:
34 33 21

Question No 3:
Input:
int main(
int arr[] = { 12,3,2,11,20,44,21,43,34,23}; int size = sizeof(arr) /
sizeof(arr[0]);
for (int i = 0; i < size; i++)
f
for (int j = 0; j < size; i++)
}
if (j>i)
arr[j] = ++1
j+= 2;
for (int i = 0; i < size; i++)
cout << arr[i]
}
Return 0;

Output:
12 1 2 11 2 5 21 3 8 23

Question No 4:
Input:
int main ()
{
int arr[] = {12,3,2,11,20,44,21,43,34, 23}; int size sizeof(arr) /
sizeof(arr[0]);
for (int i = 0; i < size; i++)
arr[i++] = i = i;
X
++i;
}
for (int i
0; i < size; i++)
{
cout << arr[i] << "
}
return 0;
}

Output:
1 3 2 16 20 44 49 43 34 100

Question No 5:
Input:
int main ()
{
int arr[] = {12,3,2,11,20,44,21,43,34,23}; int size = sizeof(arr) /
sizeof(arr[0]); int mid = (0+ size-1)/2;
arr[mid] = arr[0] + arr[size - 1];
arr[mid 1] = arr[size - 1]
-
arr[mid];
arr[mid + 1] = arr[2] arr[size-3]; for (int i = 0; i < size; i++)
}
cout << arr[i] <<
return 0;

Output:
12 3 2 -12 35 86 21 43 34 23

Question No 6:
Input:
int main()
{
{
char c[] = "C++. Practice. Assignment. int count = 0;
for (int i = 0; i < strlen(c); i++)
{
if (c[i]==’.’)
}
count++;
}
cout << "Total Sentences:
<< count;
return 0;
}

Output:
Total Sentences: 3

Question No 7:
Input:
int main()
{
int arr[] = { 12,3,2,11,28,44,21,43,34,23 }; int size = sizeof(arr) /
sizeof(arr[0]); for (int i = 0; i < size; i++)
{
for (int j = 0; j < size-1; itt)
{
if (arr[j] = arr[j+1])
{
arr[j] = arr[j]* arr[j+1]; arr[j+1] = arr[j] / arr[j+1]; arr[j] = arr[j] /
arr[j+1];
}
}
}
for (int i = 0; i < size; i++)
{
}
cout << "Element: " <«< i + 1 «< cout << arr[i] << " " << endl;
cout << endl;
int mid = (0+ size - 1) / 2;
arr[mid] = arr[0]+ arr[size - 1]; for (int i = 0; i < size; i++)
for (int j = 0; j < size 1; itt)
if (arr[j] = arr[j+1])
arr[j] = arr[j] + arr[j+1]; arr[j+1] = arr[j] = arr[j+1]; arr[j] = arr[j]
= arr[j+1];
for (int i = 0; i < size; i++)
cout<<"Element: "<1-1<< cout << arr[i] <<<<endl;
return 0;

Output:
Element: 1: 2
Element: 2: 3
Element: 3: 11
Element: 4: 12
Element: 5: 20
Element: 6: 21
Element: 7: 23
Element: 8: 34
Element: 9: 43
Element: 10: 44

Element: 1: 2
Element: 2: 3
Element: 3: 11
Element: 4: 12
Element: 5: 21
Element: 6: 23
Element: 7: 34
Element: 8: 43
Element: 9: 44
Element: 10: 46

Question No 8:
Input:
{
int main()
{
char s[] = "C++.Practice.";
for (int i = 0; i < strlen(c); i++)
c[i++] = '.';
}
8
for (int i = 0; i < strlen(c); i++)
{
cout << c[i] << " ";
}
}
return 0;

Output:
.+...P.a.t.c..

Question No 9:
Input:
int main()
{
char c[] = "sdjsd sadjfask xzcbjzz"; char temp;
int count
int length strlen(c);
for (int i = 0; i < length; i++)
{
if (c[i] != '\0')
{
char temp c[i];
int count = 0;
for (int j = 0; j H Length; j++)
{
if (temp ==c[j])
count++;
c[j] = '\0';
cout << temp
}
}
}
}
cout << " count: " << count << endl;
return 0;

Output:
s : count: 4
d : count: 3
j : count: 3
: count: 2
a : count: 2
f : count: 1
k : count: 1
x : count: 1
z : count: 3
c : count: 1
b : count: 1

Question No 10:
Input:
int main()
char c[] = "C++. Introduction to Computing.";
int length strlen(c);
for (int i = 0; i < length; i++)
[
}
if (i % 3 == 0)
c[i] = '#';
for (int i = 0; i < length; i++)
gestions: Showing
{
cout << c[i] <<
}
return 0;

Output:
*++*.I*tr*du*ti*n *o *om*ut*ng*

Question No 11:
Input:
int test(int nl, int n2)
cout << n2 << n] <<< endl; return n2*nl;
void main()
int n1-2, n2=3, n3-4;
n2=test(test(n1, n3), n2);
cout << nl << n3 << n2 << endl;

Output:
3<2
5<4
2<5<9

Question No 12:
Input:
int compute(int, int, const int[], const int[]); void main()
int c[6] (2, 3, 5, 7, 93, 4);
int d[7] = (23, 4, 9, 3, 40, 33, 1];
cout << compute(2, 4, d, c) << endl;;
int compute(int start, int end, int const a[], int const b[])
int sum = 0);
for (int i= start; i <= end; i++)
sum += b[i]-a[i];
return sum;

Output:
53
Question No 13:
Input:
int num[5] = {3, 4, 6, 2, };
int *p = num;
int *q=num + 2;
int *r=&num[1];
cout << num[2] <<" " << *(num + 2) <<<<< endl;
cout << p<<""<<(p+1)<<< endl;
cout << *q<<<"" << *(q+l) << endl;
cout << r << *(r+l) << endl;

Output:
66
34
62
46

Question No 14:
Input:
int a=2, b=3, c=4;
int *x = &a;
int *y:
*x = 11;
y=&c;
St
x=&b:
b--:
cout <<<<<<<" "<<b<< endl; cout << x <<<" " << *y <<
endl;

Output:
11 2 4
24

Question No 15:
Input:
int a[]=(1,2,3,4,5,6,7,8,9);
int *b;
int *c = &(a[1]);
b=a;
b[2] = 0;
cout << b[1] << c[0] << c[1] << a[2] << endl;

Output:
2200
Question No 16:
Input:
void swap(int left, int* right) {
int p: p = left;
left = right; right = p;
void main()
int a = 3, b = 5; swap(&a, &b);
cout << a << " " <<b<< endl;
Output:
35
Question No 17:
Write a program that inputs the number of rows
and columns from the user. It then inputs the
elements to store in the matrix. The program
calculates the sum of each row and each column
and displays on the screen. If it is a square matrix,
it also calculates the sum of diagonal elements and
displays it on screen
Output:
#include <iostream>
#include <vector>
using namespace std;

int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
vector<vector<int>> matrix(rows, vector<int>(cols));

cout << "Enter the elements of the matrix: \n";


for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
cout << "Element at position [" << i << "][" << j << "]:
";
cin >> matrix[i][j];
}
}
cout << "\nSum of each row: \n";
for (int i = 0; i < rows; ++i)
{
int rowSum = 0;
for (int j = 0; j < cols; ++j)
{
rowSum += matrix[i][j];
}
cout << "Sum of row " << i << ": " << rowSum << endl;
}
cout << "\nSum of each column: \n";
for (int j = 0; j < cols; ++j)
{
int colSum = 0;
for (int i = 0; i < rows; ++i)
{
colSum += matrix[i][j];
}
cout << "Sum of column " << j << ": " << colSum << endl;
}
if (rows == cols)
{
int diagSum = 0;
for (int i = 0; i < rows; ++i)
{
diagSum += matrix[i][i];
}
cout << "\nSum of diagonal elements: " << diagSum <<
endl;
}

return 0;
}

Question No 18:
Input:
Write a program that prompts the user to enter a
number and reverse it. Write a function Reverse()
to reverse the number. For example, if the user
enters 2765, the function should reverse it so that
it becomes 5672. The function should accept the
number as an input parameter and return the
reverse number.
Output:
#include <iostream>
using namespace std;
int Reverse(int num)
{
int reversedNum = 0;
while (num != 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}

return reversedNum;
}

int main() {
int number;
cout << "Enter a number: ";
cin >> number;
int reversed = Reverse(number);
cout << "The reversed number is: " << reversed << endl;
return 0;
}

Question No 19:
Input:
Write a program that declares a function accepting
two parameters. The first parameter is a floating
pointer number and the second parameter is an
integer. The program should multiply the floating-
point number by itself the number of times
indicated by the integer. The function should
return the result to the main function. The main
function should ask the user for the floating-point
number and integer. It should then call the
function and store the result in a variable. Finally,
the main function should display the returned.
Output:
#include <iostream>
using namespace std;
float multiplyFloatByItself(float num, int times);

int main()
{
float number;
int times;
cout << "Enter a floating-point number: ";
cin >> number;
cout << "Enter an integer: ";
cin >> times;
float result = multiplyFloatByItself(number, times);
cout << "The result is: " << result << endl;

return 0;
}
float multiplyFloatByItself(float num, int times)
{
float result = 1.0;
for (int i = 0; i < times; i++)
{
result *= num;
}

return result;
}

Question No 20:
Input:
Write a program that inputs five integers in a one-
dimensional array and passes the array to a
function. The function finds the minimum value in
the array and return to main function where it is
displayed.
Output:
#include <iostream>
using namespace std;
int findMinimum(int arr[], int size);
int main()
{
int arr[5];
cout << "Enter 5 integers: " << endl;
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
int minValue = findMinimum(arr, 5);
cout << "The minimum value in the array is: " << minValue
<< endl;

return 0;
}
int findMinimum(int arr[], int size)
{
int min = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}

return min;
}

Question No 21:
Input:
Write a function Change() that accepts an array of
integers and its size as parameters. It all array
elements by 5 that are divisible by 5 and multiplies
other array elements by 2.
Output:
#include <iostream>
using namespace std;
void Change(int arr[], int size);

int main()
{
int arr[] = {10, 3, 5, 7, 15};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
Change(arr, size);
cout << "Modified array: ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
void Change(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
if (arr[i] % 5 == 0)
{
arr[i] += 5;
} else
{
arr[i] *= 2;
}
}
}

Question No 22:
Input:
Write a program that inputs values in a 2-D array
of 5 columns and 5 rows. It displays these values
using a function It passes the array to a function
times2() that doubles the values stored in all
elements of array. The program then again
displays the changed values of the array using
display() function.
Output:
#include <iostream>
using namespace std;
void display(int arr[5][5])
{
cout << "Array Values:" << endl;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void times2(int arr[5][5])
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
arr[i][j] *= 2; // Doubling the value
}
}
}

int main() {
int arr[5][5];
cout << "Enter the values for a 5x5 array:" << endl;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << "Enter value for position [" << i << "][" << j <<
"]: ";
cin >> arr[i][j];
}
}
display(arr);
times2(arr);
cout << "\nArray values after doubling:" << endl;
display(arr);

return 0;
}

You might also like