#12 & #13 using C++ tion should create a new array that is twice the size of the
ID: 3865543 • Letter: #
Question
#12 & #13 using C++
Explanation / Answer
12.
#include<iostream>
using namespace std;
int *makelarge( int a[], int size){
int *b = new int[size+1];
b[0] = 0;
for (int i = 0; i<size; i++)
b[i+1] = a[i];
return b;
}
int main(){
int *q;
int a[] = {1,2,3,4,5,6};
q = makelarge(a,6);
for (int i = 0; i<7; i++)
cout << *(q+i) << " ";
cout << endl;
return 0;
}
13.
#include<iostream>
using namespace std;
int main(){
int n;
int *stu;
int sum = 0;
double avg;
double median;
int mode;
int *freq;
int count;
int max;
int found;
do {
cout << "Enter number of students:" << endl;
cin >> n;
} while (n < 0);
stu = new int[n];
freq = new int[n];
for (int i = 0; i<n; i++){
do {
cout << "Enter for student " << i+1 << endl;
cin >> stu[i];
} while (stu[i] < 0);
sum = sum + stu[i];
}
max = 0;
for (int i = 0; i<n; i++){
count = 0;
found = 0;
for (int j = 0; j<i; j++){
if (stu[j] == stu[i])
found = 1;
}
if (found == 1)
continue;
for (int j = 0; j<n; j++){
if (stu[i] == stu[j])
count++;
}
freq[i] = count;
if (max < count)
max = count;
}
avg = sum/n;
cout << "Average:" << avg << endl;
if (n%2 == 0)
cout << "Median:" << (stu[n/2] + stu[n/2+1])/2 << endl;
else
cout << "Median:" << stu[n/2] << endl;
cout << "Mode:";
for (int i = 0; i<n; i++){
if (freq[i] == max)
cout << stu[i] << " ";
}
cout << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.