I need help changing my current program so that the user does notenter in how ma
ID: 3612119 • Letter: I
Question
I need help changing my current program so that the user does notenter in how many number he/she will use and just starts to enterin the numbers. The program should have an array which can hold upto 50 numbers (without the user specifying this in the beginning).*Note: this only happens if the user decides to enter in thenumbers from the keyboard, not from the data file.* Here is mycode:#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int arr_2d[50][2],count;
void convert_2d(int *arr,int num)
{
int min=0,i,j,temp;
extern int count;
count =0;
for (i = 0; i < num; i++)
{
if(min>arr[i])
{
min =arr[i];
}
}
min = min-1;
for (i=0; i < num; i++)
{
if (arr[i] == min)
continue;
arr_2d[count][0] = arr[i];
arr_2d[count][1] = 1;
arr[i] = min;
for (j = i+1; j < num;j++)
{
if (arr[j] ==arr_2d[count][0])
{
arr_2d[count][1]++;
arr[j] = min;
}
}
count++;
}
for (i=0; i < count; i++)
{
for (j = i+1; j < count; j++)
{
if (arr_2d[i][0] <arr_2d[j][0])
{
temp = arr_2d[i][0];
arr_2d[i][0]=arr_2d[j][0];
arr_2d[j][0] = temp;
temp = arr_2d[i][1];
arr_2d[i][1]=arr_2d[j][1];
arr_2d[j][1] = temp;
}
}
}
}
int main()
{
int arr[50];
extern int count;
char choice;
char file[100];
int i,num;
while(1)
{
cout << "Enter choice:" << endl << "1.Keyboard" << endl << "2.File" << endl <<"Any other input will end." << endl;
cin >> choice;
switch(choice)
{
case '1':
do
{
cout << "How many numbers you want toenter: ";
cin >> num;
} while(num <= 0 || num > 50);
for (i=0; i < num ;i++)
{
cout << "Enter " << (i+1) << "num:";
cin >> arr[i];
}
break;
case '2':
{
cout << "Enter filename: ";
cin >> file;
ifstream myfile (file);
num = 0;
if (myfile.is_open())
{
while (! myfile.eof() )
{
myfile >> arr[num];
num++;
if (num >= 50)
break;
}
myfile.close();
}
else cout << "Unable to open file";
}
break;
default :
{
system("pause");
return 0;
break;
}
}
convert_2d(arr,num);
cout << endl;
cout << setw(10) << "Number" <<setw(10) << "Count" << endl << endl;
for (i=0;i<count;i++)
{
cout << setw(10) << arr_2d[i][0] <<setw(8) << arr_2d[i][1] << endl;
}
cout << endl;
}
}
Any and all help with this is greatly appreciated.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
intarr_2d[50][2],count;
void convert_2d(int*arr,int num)
{
int min=0,i,j,temp;
extern int count;
count =0;
for (i = 0; i < num; i++)
{
if(min>arr[i])
{
min =arr[i];
}
}
min = min-1;
for (i=0; i < num; i++)
{
if (arr[i] == min)
continue;
arr_2d[count][0] = arr[i];
arr_2d[count][1] = 1;
arr[i] = min;
for (j = i+1; j < num;j++)
{
if (arr[j] ==arr_2d[count][0])
{
arr_2d[count][1]++;
arr[j] = min;
}
}
count++;
}
for (i=0; i < count; i++)
{
for (j = i+1; j < count; j++)
{
if (arr_2d[i][0] <arr_2d[j][0])
{
temp = arr_2d[i][0];
arr_2d[i][0]=arr_2d[j][0];
arr_2d[j][0] =temp;
temp =arr_2d[i][1];
arr_2d[i][1]=arr_2d[j][1];
arr_2d[j][1] = temp;
}
}
}
}
int main()
{
int arr[50];
extern int count;
char choice;
char file[100];
int i,num;
while(1)
{
cout << "Enter choice:" << endl << "1.Keyboard" << endl << "2.File" << endl <<"Any other input will end." << endl;
cin >> choice;
switch(choice)
{
case '1':
for (i=0; i < 50;i++)
{
cout << "Enter " << (i+1) << "num:(<0 to exit)";
cin >> arr[i];
if(arr[i]<0)
{num=i;
break;
}
}
break;
case '2':
{
cout << "Enter filename: ";
cin >> file;
ifstream myfile (file);
num = 0;
if (myfile.is_open())
{
while (! myfile.eof() )
{
myfile >> arr[num];
num++;
if (num >= 50)
break;
}
myfile.close();
}
else cout << "Unable to open file";
}
break;
default :
{
system("pause");
return 0;
break;
}
}
convert_2d(arr,num);
cout << endl;
cout << setw(10) << "Number" <<setw(10) << "Count" << endl << endl;
for (i=0;i<count;i++)
{
cout <<setw(10) << arr_2d[i][0] << setw(8) <<arr_2d[i][1] << endl;
}
cout << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.