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

Programing Assign

The document contains 9 sections that demonstrate C++ concepts like arrays, loops, functions, pass by value vs reference using examples. Section 1 displays array elements using a for loop. Section 2 adds all array elements. Section 3 displays the largest 3 numbers in an array. Section 4 sorts an array in ascending order. Section 5 finds the most frequent element in an array. Section 6 finds the second lowest and highest numbers in an array. Section 7 checks if a word read from a file is a palindrome. Section 8 converts a number to binary and writes to a file. Section 9 explains pass by value vs reference with examples.

Uploaded by

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

Programing Assign

The document contains 9 sections that demonstrate C++ concepts like arrays, loops, functions, pass by value vs reference using examples. Section 1 displays array elements using a for loop. Section 2 adds all array elements. Section 3 displays the largest 3 numbers in an array. Section 4 sorts an array in ascending order. Section 5 finds the most frequent element in an array. Section 6 finds the second lowest and highest numbers in an array. Section 7 checks if a word read from a file is a palindrome. Section 8 converts a number to binary and writes to a file. Section 9 explains pass by value vs reference with examples.

Uploaded by

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

1.

Write a code that displays all array elements using


loop. Assignment [8] ={2,7,4,9,7,5,6,7};
#include <iostream>
using namespace std;
int main() {
int Assignment[8] = {2, 7, 4, 9, 7, 5, 6, 7};
//Display all array elements using loop
cout<<"the array elements are\n";
for (int i = 0; i < 8; i++) {

cout<< Assignment[i] << ", ";


}
/* outputs:the array elements are
2,7,4,9,7,5,6,7,
*/

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

// write binary to file


ofstream foot("D://file.txt");
if (foot.is_open()) {
foot << binary << endl;
foot.close();
cout << "Binary written to file." << endl;
} else {
cout << "Unable to open file." << endl;
}
/* output:if we enter 6:in the file we can get a binary is
110
if we enter 8:in the file we can get a binary is
1000
*/
return 0;
}
9. Write the difference between pass by value and pass by
reference using simple examples
Pass by value and pass by reference are two different
ways of passing arguments to a function or method in
programming.
Pass by value is a method of passing arguments to a
function or method where a copy of the argument's value
is passed to the function. Any changes made to the
parameter within the function do not affect the original
argument outside the function. This means that the
function cannot modify the original value of the
argument.
Pass by reference, on the other hand, is a method of
passing arguments to a function or method where a
reference or pointer to the argument's memory location is
passed to the function. Any changes made to the
parameter within the function affect the original argument
outside the function. This means that the function can
modify the original value of the argument.
In simple terms, pass by value creates a copy of the
argument, while pass by reference uses a reference to the
original argument. Pass by value is generally used for
simple data types like integers and floating-point
numbers, while pass by reference is used for
more complex data types like arrays and objects. Pass by
reference is also useful when you need to modify the
value of the original argument in a function.

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

void increment(int num) {


num++;
}
int main() {
int num = 10;
increment(num);
cout << "the number is :"<< num << endl; // prints 10
return 0;
}
//pass by reference
#include <iostream>
using namespace std;
void increment(int& num) {
num++;
}
int main() {
int num = 10;
increment(num);
cout << "the number is :"<< num << endl; // prints 11
return 0;
}

You might also like