Comp Assignment
Comp Assignment
Class : BSCS
Section : 7A
Semester : Second
int main() {
int n; // Size of the array
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " integers:" << endl;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += arr[i];
}
cout << "Sum of the elements in the array: " << sum << endl;
return 0;
}
Output:
Q2)
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the total number of elements: ";
cin >> n;
int arr[100];
cout << "Enter " << n << " integers:\n";
for (int i = 0; i < n; ++i) {
cout << "Enter Number " << i + 1 << ": ";
cin >> arr[i];
}
Output:
Q3)
#include <iostream>
int main() {
int arr[] = {8, 3, 9, 2, 6};
int N = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < N - 1; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return 0;
}
Output:
Q4)
#include <iostream>
int main() {
const int MAX_SIZE = 100;
char str[MAX_SIZE];
std::cout << "Enter a string: ";
std::cin.getline(str, MAX_SIZE);
int len = 0;
while (str[len] != '\0') {
len++;
}
return 0;
}
Output:
Q6)
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5, 6, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
if (n <= 1) {
cout << "Unique elements: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
} else {
int j = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] != arr[i + 1])
arr[j++] = arr[i];
}
arr[j++] = arr[n - 1];
Output:
Q7)
In C++, you can pass arrays as function parameters, and there are a few key differences compared to
passing individual variables. Let’s dive into the details:
1. Passing Arrays as Function Parameters:
When you pass an array to a function, you typically use the array’s name as the argument. The function
receives a pointer to the first element of the array.
When calling this function, you only use the array name as the argument
The parameter int m[5] in the function declaration converts to int* m;, pointing to the same memory
address as the original array marks. Any manipulation inside the function affects the original array
2. Passing Multidimensional Arrays:
You can also pass multidimensional arrays to functions.
Comparison with Passing Individual Variables:
a. When passing individual variables, you directly pass their values or references.
b. For arrays, you pass a pointer to the first element, which allows efficient memory usage
and avoids copying the entire array.
c. Passing arrays by reference (using references or pointers) is common in C++ to avoid
unnecessary copying.
Q8)
#include <iostream>
using namespace std;
int main() {
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int first = INT_MIN, second = INT_MIN;
Output:
Q9)
#include <iostream>
using namespace std;
int main() {
int day, month, year;
cout << "Enter the date (day month year): ";
cin >> day >> month >> year;
int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int totalDays = 0;
for (int m = 1; m < month; ++m)
totalDays += daysPerMonth[m];
totalDays += day;
cout << "Number of days from the beginning of the year to " << day << "/" << month << "/" << year
<< ": " << totalDays << endl;
return 0;
}
Output:
Q10)
#include <iostream>
using namespace std;
int main() {
const int numStudents = 5;
int rno[numStudents];
int marks[numStudents];
cout << "Enter marks for student " << i + 1 << ": ";
cin >> marks[i];
}
return 0;
}
Output:
Q11)
#include <iostream>
using namespace std;
int main() {
const int arraySize = 10;
// Initialize arrays
int numbers[arraySize];
int squares[arraySize];
int cubes[arraySize];
int sums[arraySize];
int totalSum = 0;
for (int i = 0; i < arraySize; ++i) {
totalSum += sums[i];
}
cout << "Total sum of all values in sums array: " << totalSum << std::endl;
return 0;
}
Output:
Q12)
#include <iostream>
using namespace std;
int main() {
const int numEmployees = 10;
string names[numEmployees];
double monthlySalaries[numEmployees];
Output:
Q13)
#include <iostream>
using namespace std;
int main() {
const int ARRAY_SIZE = 10;
int arr[ARRAY_SIZE];
int count[ARRAY_SIZE] = {0};
Output:
Q14)
#include <iostream>
int main() {
int marks[10];
char grades[10];
Output:
Q15)
#include <iostream>
int main() {
const int mangoPrice = 20;
const int orangePrice = 10;
const int bananaPrice = 5;
Output:
Q16)
#include <iostream>
int main() {
float num[10], total = 0, avg;
int i;
for (i = 0; i < 10; ++i) {
cout << "Enter a floating point number: ";
cin >> num[i];
total += num[i];
}
Output: