0% found this document useful (0 votes)
4 views6 pages

C++ Array Operations and Functions

The document contains multiple programming questions and their corresponding code snippets, primarily written in C++. The questions cover various topics such as array manipulation, input/output operations, sorting algorithms, and statistical calculations like mean and median. Each question includes a main function demonstrating the implementation of the provided code.

Uploaded by

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

C++ Array Operations and Functions

The document contains multiple programming questions and their corresponding code snippets, primarily written in C++. The questions cover various topics such as array manipulation, input/output operations, sorting algorithms, and statistical calculations like mean and median. Each question includes a main function demonstrating the implementation of the provided code.

Uploaded by

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

QUESTION 1

a) cout << f[4] << endl;


b) b[2] = 10;
c)
int g[4];

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


g[i] = 7;
}

d)
void total_up (double c[200]){
double sum;
for (int i=0; i<200; i++){
sum+= c[i];
cout << c[i] << " ";
}
cout << "The sum of all numbers is " << sum;
}

e)
int main()
{
double a[17], b[41];
for(int i=0; i<17; i++){
b[i] = a[i]
}

return 0;
}

f)

int main()
{
int smallest = arr[0], largest = arr[0];

for (int i=1; i<999; i++){


if (arr[i] < smallest){
smallest = arr[i];
}

if (arr[i] > largest){


largest = arr[i];
}
}

return 0;
}

QUESTION 2

a)
for (int i=0; i<10; i++){
values[i] = 0.5;
}

b)

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


if (i % 2 != 0){
values[i] = values[i] + 1;
}
}

c)

double added_values[5];

cout << "Please enter five values separated by spaces: ";


cin >> added_values[0] >> added_values[1] >> added_values[2] >> added_values[3] >>
added_values[4];

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


values[i] = added_values[i];
}

d)

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


cout << fixed << setprecision(2) << values[i] << " ";
if (i == 4){
cout << endl;
}
}

QUESTION 3

string sentence;
int vowel_count;

int main()
{
cout << "Please enter a sentence: ";
getline(cin, sentence);

for (int i=0; i<[Link](); i++){


if (sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' ||
sentence[i] == 'o' ||sentence[i] == 'u'){
vowel_count++;
}
}

cout << "The sentence you typed has " << vowel_count << " vowels!";

return 0;
}

QUESTION 4

const int arr_size = 10;


double value[arr_size];

void fillArray (double (&arr)[], int arr_size){


string input;
for (int i=0; i<arr_size; i++){
cout << "Please fill in data #" << i+1 << " (To exit enter e): ";
cin >> input;

if (input == "e"){
break;
}else{
arr[i] = stod(input);
}
}
}

void showArray (double arr[], int arr_size){


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

void reverseArray (double (&arr)[], int arr_size){


const int copied_arr_length = arr_size;
double copied_arr[copied_arr_length];

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


copied_arr[i] = arr[i];
}

for (int i=(arr_size-1); i>=0; i--){


arr[arr_size-1-i] = copied_arr[i];
}
}

int main()
{
fillArray(value, arr_size);
showArray(value, arr_size);
reverseArray(value, arr_size);

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


cout << value[i] << " ";
}

return 0;
}

QUESTION 5

#include <iostream>
#include <string>

using namespace std;


const int arr_size = 10;

double value[arr_size];

void readArray(int (&intArray)[], int counter, int inum){


cout << "Enter a set of 10 numbers separated by spaces:";

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


cin >> inum;
intArray[i] = inum;
}
}

void bubbleSort(int (&intArray)[], int counter){


for (int i = 1; i < counter; i++)
{
for (int j = 0; j < counter-i; j++)
{
if (intArray[j] > intArray[j+1])
{
int temp = intArray[j];
intArray[j] = intArray[j+1];
intArray[j+1] = temp;
}
}
}
}

double calcMean(int intArray[], int counter){


double mean=0;
for (int i = 0; i < counter; i++)
{
mean += intArray[i];
}
mean = mean/counter;

return mean;
}

int main()
{
int inum = 0, counter = 10;
int intArray[counter];
double median=0.0;

readArray(intArray, counter, inum);

if (counter%2 == 0){
median = intArray[(counter/2)-1] + intArray[counter/2];
median = median/2;
}else{
median = intArray[(counter/2)];
}

// print the results


cout << "Mean: " << calcMean(intArray, counter) << endl;
cout << "Median: " << median << endl;
}

QUESTION 6

void getMark(double (&s1)[], double (&s2)[], int arr_len){


for (int i=0; i<arr_len; i++){
cout << "Please enter student #" << i+1 <<"'s mark for subject s1: ";
cin >> s1[i];
cout << "Please enter student #" << i+1 <<"'s mark for subject s2: ";
cin >> s2[i];
}
}

void compareMark(double (&s1)[], double (&s2)[], int arr_len){


double s1_avg, s2_avg, s1_highest = s1[0], s2_highest = s2[0];
int s1_student, s2_student;

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


s1_avg += s1[i];
s2_avg += s2[i];
}

s1_avg /= arr_len;
s2_avg /= arr_len;

if (s2_avg > s1_avg){


cout << endl << "s1 (" << fixed << setprecision(1) << s1_avg << ") has a
higher average than s2 (" << fixed << setprecision(1) << s2_avg << ")" << endl;
}else{
cout << endl << "s2 (" << fixed << setprecision(1) << s2_avg << ") has a
higher average than s1 (" << fixed << setprecision(1) << s1_avg << ")" << endl;
}

for (int i=1; i<arr_len; i++){


if (s1[i] > s1_highest){
s1_highest = s1[i];
s1_student = i;
}

if (s2[i] > s2_highest){


s2_highest = s2[i];
s2_student = i;
}
};

cout << "Student " << s1_student+1 << " has the highest mark " << s1_highest <<
" in subject s1" << endl;
cout << "Student " << s2_student+1 << " has the highest mark " << s2_highest <<
" in subject s2" << endl;
}

int main()
{
double s1[6], s2[6];
getMark(s1, s2, 6);
compareMark(s1, s2, 6);

return 0;
}

You might also like