-3

I wrote a C++ program for a computer science class, but I apparently missed one thing. This is the statement that I overlooked: "Your program must include at least one function that uses one or more reference variables as parameters". How can I go about incorporating reference variables in my program and still get the same results (as my program does get the correct intended output)? I would appreciate any help with this. Thanks in advance!

#include <iostream>
#include <fstream>

using namespace std;

struct Letter_Stats
{
    int count;
    char ch;
};

void display(Letter_Stats, Letter_Stats);

int main()
{
    ifstream inFile;
    char ch;
    int i;
    Letter_Stats letters[26];
    Letter_Stats small, large;

    inFile.open("letter_count.txt", ios::in);

    if (inFile.fail())
    {
        cout << "Error opening file.";
        return 1;
    }

    for (i = 0; i<26; i++)
    {
        letters[i].count = 0;
        letters[i].ch = ('A' + i);
    }

    while (!inFile.eof())
    {
        inFile.get(ch);

        if ('A' <= toupper(ch) && toupper(ch) <= 'Z')
            letters[toupper(ch) - 'A'].count++;
    }

    inFile.close();
    small = letters[0];
    large = letters[0];

    for (i = 1; i<26; i++)
    {
        if (letters[i].count > large.count)
        {
            large = letters[i];
        }

        if (letters[i].count < small.count)
        {
            small = letters[i];
        }
    }

    display(large, small);
    return 0;
}

void display(Letter_Stats most, Letter_Stats least)
{
    cout << "The most common letter is " << most.ch << " with " << most.count << " occurrences." << endl;
    cout << "The least common letter is " << least.ch << " with " << least.count << " occurrences." << endl;
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Juan
  • 11
  • 1
  • 6

1 Answers1

1

just change:

void display(Letter_Stats, Letter_Stats);

to:

void display(Letter_Stats&, Letter_Stats&);

and:

void display(Letter_Stats most, Letter_Stats least)

to:

void display(Letter_Stats &most, Letter_Stats &least)

Reference means that you don't send the value to the function as a copy, you send the variable's address to the function. The advantages that you get from it (in most of the cases):

  1. Usually, your function will be less heavy, because you send only 8 bytes of memory address and not the whole variable (pay attention that in your case your variable take only 5 bytes - int + char, but whenever you will want to add another array, or even an int to your struct, reference will be smaller).
  2. In your case it doesn't really meters, but you can change the variables' that you send into the function values without return them (and by this way effect from the function on more then one variable that outside the function).

EDIT:

Because you don't need to change those variables values, you can use the follow function's signature, and send the variables as const reference, as @M.M mention in the comment:

void display(const Letter_Stats&, const Letter_Stats&);
void display(const Letter_Stats &most, const Letter_Stats &least)
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
  • 2
    `const&` instead of `&` would be an improvement – M.M Sep 27 '18 at 01:43
  • If pointers are 8 bytes (as is common these days) then case 1 does not hold up; and this function intentionally does not change the values so case 2 doesn't hold up either. IMO it is better not to use reference variables (if we weren't doing an assignment that requires it) – M.M Sep 27 '18 at 01:45
  • @M.M I ran a test, and printed int& size, and int size, the results were the same- 4. Ubuntu 16.04 gcc compiler, OS 64 bit. In this case int+char in struct will give me sizeof = 5. Which means that case 1 does hold up. – Coral Kashri Sep 27 '18 at 01:56
  • You can't print the size used by passing a reference. If you did `sizeof(size)` then that is `sizeof(int)`. Try checking the assembly generated. Also I bet you the size of the struct is not `5` – M.M Sep 27 '18 at 02:13
  • Emm you right. but still, in this particular case it's better not to use reference- but usually it's better to use it, and it something that every c++ programmer have to get used to it. 3 bytes (maximum 4) in particular case against hundreds of thousands in other cases, I think that case 1 still hold.. Case 2, as mentioned in my response, is less relevant here, but relevant to reference in general- thing that makes the response more relevant for the OP. – Coral Kashri Sep 27 '18 at 02:27