Begginer Programming

Hello Anyone,

I have the following homework question and was wondering if I could get some help on it. I am very new to programming as this is my first class in it. Any help would be greatly appreciated!

Write a program that reads in an array of type int. Provide facility to either read this array from keyboard or from a file, at the user’s option. The output for the file input option should be stored in a file while the output for the keyboard input option should be displayed on the computer screen. If the user chooses file input, the program should request file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be a two column list. The first column is a list of distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest.

thanks,
Joe
It's part of the forum rules that you cant ask for people to do your homework. If you just wanto help show what you have already done.
Ok, thank you. I'll show what i have shortly.
This is what i have so far. Am i going in the right direction? i believe i need to do the void sort part of the code next and then the num_count... a little is unfinished for reading the numbers in.





#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

const int DECLARED_SIZE = 50;

void fill_array(int n[], int size, int&number_used);

void sort(int n[], int number_used);

void num_count(int n[], int number_used);


int main()
{
int size[DECLARED_SIZE], number_used;
int ans;

cout << "This program will read integers from a file or keyboard input then \n"
<< "sorts from biggest to smallest and counts how many times \n"
<< "an integer is used.\n";

cout << "Enter in numbers using the keyboard or a file: \n";

return 0;

void fill_array(int n[], int size, int number_used);
{
char ans;
char in_file_name[15];
ifstream in_stream;

cout << "How will you be entering input?" << endl;
cout << "Press 1 for keyboard or 2 for file." << endl;
{
if (ans == '1');
{
cout << "Enter up to " << size << " numbers." << endl;
cin >> size[];

for (n = 1, n < 50, n++);
}

else (ans == '2');
{
cout << "Please enter a file name with less than 15 characters: " << endl;
cin >> in_file_name;

in_stream.open(in_file_name);
if (in_stream.fail())
{
cout << "Inputing file failed!";
exit(1);
}


}

Last edited on
You cannot put a function definition inside of another function (such as fill_array inside main.) You're missing brackets and the code is not compilable.

Also, use code tags. The <> button to the right under Format: when you're editing a post.

You need to review the way you index arrays. For instance size[50] when size is an arraying holding 50 elements is outside the bounds of the array. You also need to review the semantics of for loops.
Some of that i haven't finished. I'm working on that part now and will update my previous post.

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

const int DECLARED_SIZE = 50;

void fill_array(int n[], int size, int&number_used);

void sort(int n[], int number_used);

void num_count(int n[], int number_used);


int main()
{
int size[DECLARED_SIZE], number_used;
int ans;

cout << "This program will read integers from a file or keyboard input then \n"
<< "sorts from biggest to smallest and counts how many times \n"
<< "an integer is used.\n";

cout << "Enter in numbers using the keyboard or a file: \n";

return 0;

void fill_array(int n[], int size, int number_used);
{
char ans;
char in_file_name[15];
ifstream in_stream;
int n, size[50], number_used;

cout << "How will you be entering input?" << endl;
cout << "Press 1 for keyboard or 2 for file." << endl;
{
if (ans == '1');

cout << "Enter up to " << size << " numbers: \n";

for (n = 0; n < size; n++)
cin >> size[n];
else if (ans == '2');
{
cout << "Please enter a file name with less than 15 characters: " << endl;
cin >> in_file_name;

in_stream.open(in_file_name);
if (in_stream.fail())
{
cout << "Inputing file failed!";
exit(1);
}


}

Last edited on
Topic archived. No new replies allowed.