Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello! I have a program I am creating and I am having trouble with pointers. The

ID: 3631717 • Letter: H

Question

Hello!

I have a program I am creating and I am having trouble with pointers. The purpose of this program to replace the static arrays with dynamically allocated arrays. I am having difficulty with the char 2d array. Here is main:

int main()
{
    int numCandidates;
    char **allCandidates;
    int *votes, index, totalVotes;
    ifstream infile;

    infile.open("./Ch9_Ex7Data.txt");
    if (!infile)
    {
        cerr << "Cannot open input file. Program terminates!" << endl;
        return 1;
    }

// read number of candidates

    readVotes (infile, votes, allCandidates, numCandidates);

    cout << fixed << showpoint << setprecision(2);

    infile.close();

    totalVotes = sumVotes(votes, numCandidates);

    cout << left << setw(40) << "Candidate" << "Votes Received "
          << "   % of Total Votes" << endl;

    for (index = 0; index < numCandidates; index++)
        cout << left << setw(40) << allCandidates[index]
            << right << " " << setw(10) << votes[index] << "   " << setw(15)
            << (static_cast<double>(votes[index]) / static_cast<double>(totalVotes)) * 100 << endl;

    cout << " Total: " << totalVotes << endl;

    cout << "Winner: " << allCandidates[winnerIndex(votes,numCandidates)] << endl;
    system ("Pause");
    return 0;
}


and here is the read votes function

void readVotes (ifstream & infile, int * & votes,
                char ** & allCandidates, int & numCandidates)
{

// read number of candidates
    infile >> numCandidates;
    infile.ignore(); // carriage return

    votes = new int [numCandidates];

    allCandidates = new char *[numCandidates];


    for (int index = 0; index < numCandidates; index++)
    {
        infile >> votes[index];
        infile.ignore(); // space
        infile.get(allCandidates[index], MAX_NAME_LEN);
    }

here is the sumVotes function

int sumVotes(const int list[], int size)
{
    int sum = 0;

    for (int i = 0; i < size; i++)
        sum += list[i];

    return sum;
}

winner function

int winnerIndex(const int list[], int size)
{
    int maxIndex = 0;

    for (int i = 1; i < size; i++)
        if (list[i] > list[maxIndex])
            maxIndex = i;
    return maxIndex;
}

and the header

#ifndef Ch9_Ex7_h
#define Ch9_Ex7_h

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int const MAX_NAME_LEN = 31; // includes NULL

int extern sumVotes(const int [], int);
int extern winnerIndex(const int [], int);
void extern readVotes (ifstream &, int * &, char ** &, int &);

#endif

If anyone has any suggestions, that would help alot!

Explanation / Answer

* is used to denote a pointer. & is used to get the pointer to a variable. You don't need to pass a pointer if you're not changing the value of the variable.

void readVotes (ifstream infile, int* votes, char** allCandidates, int numCandidates);

When you dynamically allocate a 2 dimensional array, first allocate the first dimension, then loop and allocate the second.

char** array;
int n, m;

array = new char* [n];

for(int i = 0; i < n; i++)
array[i] = new char[m];

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote