Programing Assign
Programing Assign
return 0;
}
2.After you access all array elements using loop, now try
to add all elements.
#include <iostream>
using namespace std;
int main() {
int Assignment[8] = {2, 7, 4, 9, 7, 5, 6, 7};
// Add all elements of the array
int sum = 0;
for (int i = 0; i <= 7; i++) {
sum += Assignment[i];
}
cout << "Sum of all elements: " << sum << endl;
//output: sum of all elements: 47
return 0;
}
3.From the above array, display the largest three
maximum numbers.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int Assignment[8] = {2, 7, 4, 9, 7, 5, 6, 7};
// Display the largest three maximum numbers
sort(Assignment, Assignment + 8, greater<int>());
cout << "Largest three maximum numbers:\n ";
for (int i = 0; i <=2; i++) {
cout << Assignment[i] << " ,";
}
cout << endl;
//output: 9,7,7,
return 0;
}
4.From the above array, sort the elements in ascending
order.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int Assignment[8] = {2, 7, 4, 9, 7, 5, 6, 7};
//Sort the array in ascending order
sort(Assignment,Assignment + 8 );
cout << "Sorted array in ascending order:\n ";
for (int i = 0; i <=7; i++) {
cout << Assignment[i] << ", ";
}
cout << endl;
//output:Sorted array in ascending order:
2,4,5,6,7,7,7,9,
return 0;
}
5.From the above array, find the most frequent element.
#include <iostream>
using namespace std;
int main(){
int Assignment[8] = {2, 7, 4, 9, 7, 5, 6, 7};
//Find the most frequent element
cout << "Most frequent element from above array is :\n
";
int freq[8] = {0};
for (int i = 0; i <=7; i++) {
freq[Assignment[i]-1]++;
}
int max_freq = freq[0], most_frequent = 1;
for (int i = 1; i < 8; i++) {
if (freq[i] > max_freq) {
max_freq = freq[i];
most_frequent = i + 1;
}
}
cout<< most_frequent << endl;
//output:Most frequent element from above array is: 7
return 0;
}
6.Write a C++ program to find the second lowest and
highest numbers in the above array.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int Assignment[8] = {2, 7, 4, 9, 7, 5, 6, 7};
const int size = sizeof(Assignment) /
sizeof(Assignment[0]);
// Sort the array in ascending order
sort(Assignment, Assignment + size);
// Find the second lowest and second highest elements
int secondLowest = Assignment[1];
int secondHighest = Assignment[size - 2];
// Print the results
cout << "Second lowest: " << secondLowest <<
std::endl;
cout << "Second highest: " << secondHighest <<
std::endl;
//output:Second lowest: 4 and Second highest: 7
return 0;
}
7.Read a word from the file and check whether the word
is palindrome or not.
#include<iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main() {
string word;
ifstream file("D://file.txt");
if (file.is_open()) {
file >> word;
file.close();
string reversed_word = word;
reverse(reversed_word.begin(),
reversed_word.end());
// Check if a word from file is palindrome
if (word == reversed_word) {
cout << "The word is a palindrome." << endl;
} else {
cout << "The word is not a palindrome." << endl;
}
} else {
cout << "Unable to open file." << endl;
}
/*output: if in the file a word is "444" or
"did" :The word is a palindrome.
if in the file a word is "423" or "work" :The
word is not a palindrome.
*/
return 0;
}
8.Write a code that converts a given number to binary
numbering system. And send the binary number to the
file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
// convert number to binary
string binary = "";
while (num > 0) {
if (num % 2 == 0)
binary = "0" + binary;
else
binary = "1" + binary;
num /= 2;
}
//pass by value
#include <iostream>
using namespace std;
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int sum = add(num1, num2);
cout << "num1 is: " << num1 << endl;
cout << "num2 is: " << num2 << endl;
cout <<"the sum of two numbers are:"<< "Sum is: " <<
sum << endl;
return 0;
}
//pass by reference
#include <iostream>
using namespace std;
void add(int &num1, int &num2,int &sum ) {
sum = num1 + num2;
}
int main() {
int num1, num2, sum = 0;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
add(num1, num2,sum);
cout << "num1 is: " << num1 << endl;
cout << "num2 is: " << num2 << endl;
cout <<"the sum of two numbers are:"<< "Sum is: " <<
sum << endl;
return 0;
}
BY OTHE R EXAMPLES
//pass by value
#include <iostream>
using namespace std;