Lab sheet 5 with answer
Lab sheet 5 with answer
Solution
int SIZE = 5;
int numbers[SIZE];
int sum = 0;
sum += numbers[i];
///////
int arr[5];
int sum=0;
cout<<"enter element"<<i+1<<endl;
cin>> arr[i];
Solution
int SIZE=5;
int arr[]={1,2,5,88,6};
int max1d=arr[0];
for(int i=0;i<SIZE;++i){
max1d=arr[i];
cout << "Maximum element in the 1D array: " << max1d << endl;
////////////////////////////////
int N=3;
int M=4;
int arr[N][M]={
{545,5,6,4},
{4,5,5,8}
};
int max2d=arr[0][0];
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(arr[i][j]>max2d){
max2d=arr[i][j];
cout << "Maximum element in the 2D array: " << max2d << endl;
solution
string s;
cin>> s;
int i=0;
while(s[i]!='\0'){
//cout<<s[i];
i++;
cout<<i;
Solution
int currFreq = 1;
if (myArray[j] == currNum)
++currFreq;
repeatedNum = currNum;
maxFrequency = currFreq;
if (repeatedNum != -1)
else
5. Tracing
What is the output?
Result
int main() {
return 0;
}
The output
#include <iostream>
using namespace std;
int main() {
double sum = 0;
double count = 0;
return 0;
}
Run Code
Output
7
1 CSC1100: Computer Programming I 2024
9.
int main() {
int numbers[2][3];
9
cout << "Enter 6 numbers: " << endl;
return 0;
}
Run Code
Output
Enter 6 numbers:
1
2
3
4
5
6
The numbers are:
numbers[0][0]: 1
numbers[0][1]: 2
numbers[0][2]: 3
numbers[1][0]: 4
numbers[1][1]: 5
numbers[1][2]: 6
Sample Solution:
C++ Code :
#include<iostream> // Header file for input/output stream
11
int main()
return 0;
Sample Output:
Original array: 4 5 9 12 9 22 45 7
Wave form of the said array: 5 4 9 7 12 9 45 22
Flowchart:
Visual Presentation:
C++ Code :
#include <iostream>
#include <string>
if (array_size == 2)
else
else
bool flag;
int temp;
do
flag = false;
temp = array_nums[x];
array_nums[x] = array_nums[x
+ 1];
array_nums[x + 1] = temp;
flag = true;
} while (flag);
int index = 0;
index++;
else
break;
index2--;
else
break;
// Main function
int main() {
Second_highest_lowest(nums1, size_A );
size_A = sizeof(nums2)/sizeof(nums2[0]);
Second_highest_lowest(nums2, size_A );
size_A = sizeof(nums3)/sizeof(nums3[0]);
Second_highest_lowest(nums3, size_A );
int nums4[] = { 9, 9 , 9, 9, 9 };
size_A = sizeof(nums4)/sizeof(nums4[0]);
Second_highest_lowest(nums4, size_A );
Sample Output:
Array elements: 9 122 12 1
Second lowest number of the said array: 9
Array elements: 9 12 12 1
Second lowest number of the said array: 9
Second highest NUmber of the said array: 9
Array elements: 5 5 9 9 12 12 1
Second lowest number of the said array: 5
Second highest NUmber of the said array: 9
Array elements: 9 9 9 9 9
Second lowest number of the said array: 0
Second highest NUmber of the said array: 0
13. Write a C++ program to find and print all distinct elements of a
given array of integers.
Visual Presentation:
.
// C++ Program to display all elements
// of an initialised two dimensional array
#include <iostream>
using namespace std;
int main() {
int test[3][2] = {{2, -5},
return 0;
}
Run Code
Output
test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1
int main() {
int numbers[2][3];
return 0;
}
Run Code
Output
Enter 6 numbers:
1
2
3
4
5
6
The numbers are:
numbers[0][0]: 1
numbers[0][1]: 2
numbers[0][2]: 3
numbers[1][0]: 4
numbers[1][1]: 5
numbers[1][2]: 6
Visual Presentation:
22
1 CSC1100: Computer Programming I 2024
17. Write a C++ program to find the number of pairs of
integers in a given array of integers whose sum is equal to a
specified number.
C++ Code :
23
1 CSC1100: Computer Programming I 2024
for (int i = 0; i < s1; i++) // Loop to output elements of
the original array
cout << "\nArray pairs whose sum equal to 12: "; // Output
label for pairs whose sum equals 12
Sample Output:
Original array: 1 5 7 5 8 9 11 12
Array pairs whose sum equal to 12:
1,11
5,7
7,5
Number of pairs whose sum equal to 12: 3
C++ Code :
#include <iostream> // Header file for input/output stream
Sample Output:
Original array: 1 5 7 5 8 9 11 12
Array pairs whose sum equal to 12:
1,11
5,7
19. Write a C++ program to find the third largest string in a given
array of strings.
C++ Code :
#include <iostream> // Including the Input/Output Stream
Library
if (len2 == len3) {
} else {
// Main function
int main() {
// Arrays of strings
Sample Output:
Array elements: abcd abcde abcdef abcdefg abcdefgh
Third highest length string: abcdef
Visual Presentation: