Comp Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Name : Bilawal

Class : BSCS

Section : 7A

Semester : Second

Teacher : Sir Abubakar


Q1)
#include <iostream>
using namespace std;

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

int max = arr[0];


for (int i = 1; i < n; ++i) {
if (arr[i] > max)
max = arr[i];
}

cout << "\nLargest element = " << max;


return 0;
}

Output:
Q3)
#include <iostream>

using namespace std;

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

cout << "Sorted array:" << endl;


for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
cout << endl;

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

for (int i = 0; i < len / 2; ++i) {


char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}

std::cout << "Reversed string: " << str << std::endl;

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

cout << "Unique elements: ";


for (int i = 0; i < j; i++)
cout << arr[i] << " ";
}
return 0;
}

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;

for (int i = 0; i < n; i++) {


if (array[i] > first) {
second = first;
first = array[i];
}
else if (array[i] > second && array[i] < first) {
second = array[i];
}
}
cout << "Second Largest Element in the Array: " << second << endl;
return 0;
}

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

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


daysPerMonth[2] = 29;

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

for (int i = 0; i < numStudents; ++i) {


cout << "Enter roll number for student " << i + 1 << ": ";
cin >> rno[i];

cout << "Enter marks for student " << i + 1 << ": ";
cin >> marks[i];
}

int highestMarks = marks[0];


int highestMarksIndex = 0;
for (int i = 1; i < numStudents; ++i) {
if (marks[i] > highestMarks) {
highestMarks = marks[i];
highestMarksIndex = i;
}
}
cout << "Student with highest marks:" << endl;
cout << "Roll Number: " << rno[highestMarksIndex] << endl;
cout << "Marks: " << marks[highestMarksIndex] << endl;

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

for (int i = 0; i < arraySize; ++i) {


numbers[i] = i;
squares[i] = i * i;
cubes[i] = i * i * i;
sums[i] = numbers[i] + squares[i] + cubes[i];
}

cout << "Sums array values:" << std::endl;


for (int i = 0; i < arraySize; ++i) {
cout << "sums[" << i << "] = " << sums[i] << std::endl;
}

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

for (int i = 0; i < numEmployees; ++i) {


cout << "Enter name of employee " << i + 1 << ": ";
cin >> names[i];
cout << "Enter monthly salary for " << names[i] << ": ";
cin >> monthlySalaries[i];
}

for (int i = 0; i < numEmployees; ++i) {


double annualSalary = monthlySalaries[i] * 12;
cout << "Employee: " << names[i] << endl;
cout << "Monthly Salary: Rs " << monthlySalaries[i] << endl;
if (annualSalary >= 250000) {
cout << "Tax to be paid" << endl;
} else {
cout << "No tax" << endl;
}
cout << endl;
}
return 0;
}

Output:
Q13)
#include <iostream>
using namespace std;

int main() {
const int ARRAY_SIZE = 10;
int arr[ARRAY_SIZE];
int count[ARRAY_SIZE] = {0};

cout << "Enter ten integers:" << endl;


for (int i = 0; i < ARRAY_SIZE; ++i) {
cin >> arr[i];
count[arr[i]]++;
}

cout << "Occurrences of each number in the array:" << endl;


for (int i = 0; i < ARRAY_SIZE; ++i) {
if (count[i] > 0) {
cout << "• " << i << " is stored " << count[i] << " times in the array." << endl;
}
}
return 0;
}

Output:
Q14)
#include <iostream>

using namespace std;

int main() {
int marks[10];
char grades[10];

for (int i = 0; i < 10; ++i) {


cout << "Enter marks for student " << i + 1 << ": ";
cin >> marks[i];

if (marks[i] >= 80) {


grades[i] = 'A';
} else if (marks[i] >= 60) {
grades[i] = 'B';
} else if (marks[i] >= 40) {
grades[i] = 'C';
} else {
grades[i] = 'F';
}
}

cout << "\nGrades for each student:\n";


for (int i = 0; i < 10; ++i) {
cout << "Student " << i + 1 << ": " << grades[i] << endl;
}
return 0;
}

Output:
Q15)
#include <iostream>

using namespace std;

int main() {
const int mangoPrice = 20;
const int orangePrice = 10;
const int bananaPrice = 5;

int Mango[100], Orange[100], Banana[100];


int numCustomers;

cout << "Enter the number of customers: ";


cin >> numCustomers;

for (int i = 0; i < numCustomers; ++i) {


cout << "Customer " << i + 1 << ":\n";
cout << "Enter the number of mangos: ";
cin >> Mango[i];
cout << "Enter the number of oranges: ";
cin >> Orange[i];
cout << "Enter the number of bananas: ";
cin >> Banana[i];
}

cout << "\nTotal Bill for Each Customer:\n";


for (int i = 0; i < numCustomers; ++i) {
int totalBill = Mango[i] * mangoPrice + Orange[i] * orangePrice + Banana[i] * bananaPrice;
cout << "Customer " << i + 1 << ": Rs. " << totalBill << "\n";
}
return 0;
}

Output:

Q16)
#include <iostream>

using namespace std;

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

avg = total / 10;

for (i = 0; i < 10; ++i) {


if (num[i] > avg) {
cout << "\n" << num[i] << " is greater than the average value of the array.";
}
}
return 0;
}

Output:

You might also like