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

Group assignment fundamental programming

Group members
Abdirahman jamac 1406152
Abdikalik faysal 1406158
Rufai sharif 1406145
Sirika wakgari 1406162
Abdishakur abdi 1406118

Question 1
#include <iostream>
#include <vector>
std::pair<std::vector<int>, std::vector<int>> separatePositiveNegative(const std::vector<int>&
arr) {
std::vector<int> positive;
std::vector<int> negative;

for (int num : arr) {


if (num >= 0)
positive.push_back(num);
else
negative.push_back(num);
}

return {positive, negative};


}

int main() {
int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;

std::vector<int> nums(size);

std::cout << "Enter the elements of the array: ";


for (int i = 0; i < size; ++i) {
std::cin >> nums[i];
}

std::pair<std::vector<int>, std::vector<int>> result = separatePositiveNegative(nums);

std::cout << "Positive values: ";


for (int num : result.first) {
std::cout << num << " ";
}
std::cout << std::endl;

std::cout << "Negative values: ";


for (int num : result.second) {
std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}

Question 2:
#include <iostream>
#include <algorithm>

const int MAX_SIZE = 100;

void inputArray(int n, int arr[]) {


std::cout << "Enter " << n << " integers:" << std::endl;
for (int i = 0; i < n; ++i) {
std::cin >> arr[i];
}
}

bool findNumber(int n, int arr[], int num) {


for (int i = 0; i < n; ++i) {
if (arr[i] == num) {
return true;
}
}
return false;
}

int calculateSum(int n, int arr[]) {


int sum = 0;
for (int i = 0; i < n; ++i) {
sum += arr[i];
}
return sum;
}

void sortArray(int n, int arr[]) {


std::sort(arr, arr + n);
}

int findLargest(int n, int arr[]) {


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

void outputArray(int n, int arr[]) {


std::cout << "Array: ";
for (int i = 0; i < n; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}

void outputReverse(int n, int arr[]) {


std::cout << "Array in reverse order: ";
for (int i = n - 1; i >= 0; --i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}

int main() {
int n;
int arr[MAX_SIZE];

std::cout << "Enter the number of integers (1 - " << MAX_SIZE << "): ";
std::cin >> n;

inputArray(n, arr);

int num;
std::cout << "Enter a number to find: ";
std::cin >> num;
bool found = findNumber(n, arr, num);
std::cout << "Number " << num << (found ? " is" : " is not") << " found in the array." <<
std::endl;

int sum = calculateSum(n, arr);


std::cout << "Sum of the array elements: " << sum << std::endl;

sortArray(n, arr);
outputArray(n, arr);

int largest = findLargest(n, arr);


std::cout << "Largest element in the array: " << largest << std::endl;

outputReverse(n, arr);

return 0;

Question 3
#include <iostream>

using namespace std;

const int MAX_COURSES = 5; // Maximum number of courses


void fillMark(int marks[], int numCourses) {
for (int i = 0; i < numCourses; i++) {
cout << "Enter mark for Course " << (i + 1) << ": ";
cin >> marks[i];
}
}

void displayMark(const int marks[], int numCourses) {


cout << "Marks for " << numCourses << " courses:\n";
for (int i = 0; i < numCourses; ++i) {
cout << "Course " << (i + 1) << ": " << marks[i] << endl;
}
}

void replaceMark(int marks[], int numCourses) {


int courseNum, newMark;
cout << "Enter course number to replace mark: ";
cin >> courseNum;

if (courseNum >= 1 && courseNum <= numCourses) {


cout << "Enter new mark for Course " << courseNum << ": ";
cin >> newMark;
marks[courseNum - 1] = newMark;
cout << "Mark for Course " << courseNum << " replaced successfully.\n";
} else {
cout << "Invalid course number!\n";
}
}

int main() {
int marks[MAX_COURSES];
int numCourses = MAX_COURSES;

fillMark(marks, numCourses);

char choice;
do {
cout << "\nMenu:\n";
cout << "(D)isplay, (R)eplace, Re(F)ill, or (Q)uit?\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 'D':
case 'd':
displayMark(marks, numCourses);
break;
case 'R':
case 'r':
replaceMark(marks, numCourses);
break;
case 'F':
case 'f':
fillMark(marks, numCourses);
break;
case 'Q':
case 'q':
cout << "Exiting the program...";
break;
default:
cout << "Invalid choice! Please try again.\n";
break;
}
} while (choice != 'Q' && choice != 'q');

return 0;
}
Question 4
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_SIZE = 20;

void displayArray(int arr[], int size) {


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

bool replaceNum(int arr[], int size, int numToSearch, int replacement) {


for (int i = 0; i < size; i++) {
if (arr[i] == numToSearch) {
arr[i] = replacement;
return true;
}
}
return false;
}

void fillArray(int arr[], int size) {


for (int i = 0; i < size; i++) {
cout << "Enter element " << (i + 1) << ": ";
cin >> arr[i];
}
}

int main() {
int arr[MAX_SIZE];
int size, replacement, numToSearch;
char option;

cout << "Enter a positive number no larger than 20: ";


cin >> size;

while(size <= 0 || size > MAX_SIZE) {


cout << "Invalid input. Enter a positive number no larger than 20: ";
cin >> size;
}

fillArray(arr, size);

do {
cout << "(D)isplay, (R)eplace, Re(F)ill, or (Q)uit? ";
cin >> option;
option = tolower(option);
switch (option) {
case 'd':
displayArray(arr, size);
break;
case 'r':
cout << "Enter a number to search for: ";
cin >> numToSearch;
cout << "Enter replacement: ";
cin >> replacement;

if (replaceNum(arr, size, numToSearch, replacement)) {


cout << "Replacement successful." << endl;
} else {
cout << "Replacement unsuccessful." << endl;
}
break;
case 'f':
fillArray(arr, size);
break;
case 'q':
cout << "Good bye!" << endl;
break;
default:
cout << "Invalid answer - input again!" << endl;
break;
}
} while (option != 'q');

return 0;
}

You might also like