Expert, there are two questions below that I have answered. Please check to see if my answers are correct and if not please give correct answer/s. 1. Find the mode's count? if (a[idx] > a[max_idx]) max_idx = idx; return max_idx; 2. Find the values whose counts equal the mode's count? if (counts[idx] == mode) cout << ' ' << "mode"; cout << endl;
Addition of Two Numbers
Adding two numbers in programming is essentially the same as adding two numbers in general arithmetic. A significant difference is that in programming, you need to pay attention to the data type of the variable that will hold the sum of two numbers.
C++
C++ is a general-purpose hybrid language, which supports both OOPs and procedural language designed and developed by Bjarne Stroustrup. It began in 1979 as “C with Classes” at Bell Labs and first appeared in the year 1985 as C++. It is the superset of C programming language, because it uses most of the C code syntax. Due to its hybrid functionality, it used to develop embedded systems, operating systems, web browser, GUI and video games.
The example program below finds multiple modes in two steps:
#include <cassert>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
const string empty_string = "";
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int nalphas = 26;
int max(int a[], int size);
int main()
{
int* counts = new int[nalphas];
for (int idx = 0; idx < nalphas; idx++)
counts[idx] = 0;
cout << "Enter input file name: ";
string file_name;
getline(cin, file_name);
ifstream finp;
finp.open(file_name);
assert(finp.good());
string line;
int alpha_count = 0;
while (!getline(finp, line).eof())
{
for (int idx = 0; idx < line.size(); ++idx)
{
char target = toupper(line[idx]);
int alpha_idx = alphabet.find(target);
if (alpha_idx >= 0)
{
counts[alpha_idx]++;
alpha_count++;
}
}
}
finp.close();
int mode = counts[max(counts, nalphas)];
int fwidth = static_cast<int>(log10(alpha_count)) + 1;
for (int idx = 0; idx < nalphas; ++idx)
{
cout << alphabet[idx] << ' ' << setw(fwidth) << counts[idx];
if (counts[idx] == mode)
cout << ' ' << "mode";
cout << endl;
}
cout << " " << setw(fwidth) << alpha_count << endl;
system("pause");
return 0;
}
int max(int a[], int size)
{
if (size < 1)
return -1;
int max_idx = 0;
for (int idx = 1; idx < size; ++idx)
if (a[idx] > a[max_idx])
max_idx = idx;
return max_idx;
}
-----------------------------------------------------------------------------------
Expert, there are two questions below that I have answered.
Please check to see if my answers are correct and if not please give correct answer/s.
1. Find the mode's count?
if (a[idx] > a[max_idx])
max_idx = idx;
return max_idx;
2. Find the values whose counts equal the mode's count?
if (counts[idx] == mode)
cout << ' ' << "mode";
cout << endl;
Trending now
This is a popular solution!
Step by step
Solved in 2 steps