Help! Struggling to Grasp Arrays and Strings for Final Exam

In summary: Your final exam is tuesday and you are having an extremely extremely hard time grasping arrays and strings. " << endl; cout << "I just can't visualize how they work and I am stuck on how to get my printOwnedShares function to work properly in a project that will help me study for the final. This is what I have worked on (its taking me over 30 hours and I've been trying really hard):" << endl; << printOwnedShares( "My final exam is tuesday and I am having an extremely hard time grasping arrays and strings.", shares, 1, MAX - 1 ); << endl; return 0;
  • #1
My final exam is tuesday and I am having an extremely extremely hard time grasping arrays and strings. I just can't visualize how they work and I am stuck on how to get my printOwnedShares function to work properly in a project that will help me study for the final. This is what I have worked on (its taking me over 30 hours and I've been trying really hard):

Code:
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

// PROTOTYPE FUNCTIONS
void printMenu(string name[], double price[], int size);
int getCompany(string name[], double price[], int size);
double getShares(string name);
double calculateCost(char stock, int shares);
double calculateCommission(double shares, double cost);
void printPurchase(double shares, double cost,
                    double commission, string name);
bool anotherTransaction();
void printSummary(int purchases, double total_cost,
                               double total_commission);
// Project 8
double getPrice (string name);
void getMenu (string name[], double price[], int size);
void printOwnedShares (string name[], double shares[], int size);

int main ( )
{

    // SET PRECISION
     cout.setf(ios::fixed, ios::floatfield);
     cout.precision(2);
     cout.setf(ios::showpoint);

    /* **********************************
    Declare Symbolic Constants
    ********************************** */
     const int MAX = 8;

    /* **********************************
    Declare Variables
    ********************************** */
    int stock;
    bool transaction;
    double cost, commission;
    /* ********* PROJECT 8 */
    int index;
    double price[MAX];
    double shares[MAX];
    string name[MAX];

    int current_size = 1;
    int purchases = 0;
    double total_cost = 0.0;
    double total_commission = 0.0;

    /* **********************************
    Input
    ********************************** */
     cout << "Welcome to FT's Stockbroker Program." << endl;

     // PROJECT 8
     getMenu(name, price, MAX);

     // ANOTHER TRANSACTION
      transaction = anotherTransaction();
        while(transaction == true)
        {
          // NUMBER OF PURCHASES
          purchases = purchases + 1;

     // RETURN CHOICE OF COMPANY
      stock = getCompany(name, price, MAX);

     // RETURN NUMBER OF SHARES
      if (stock == 1)
        shares[index] = getShares(name[0]);
      else if (stock == 2)
        shares[index] = getShares(name[1]);
      else if (stock == 3)
        shares[index] = getShares(name[2]);
      else if (stock == 4)
        shares[index] = getShares(name[3]);
      else if (stock == 5)
        shares[index] = getShares(name[4]);
      else if (stock == 6)
        shares[index] = getShares(name[5]);
      else if (stock == 7)
        shares[index] = getShares(name[6]);
      else
        shares[index] = getShares(name[7]);

      // PRINT CURRENTLY OWNED SHARES
      printOwnedShares(name, shares, current_size);
          current_size = current_size + 1;

    /* **********************************
    Compute
    ********************************** */
     // CALCULATE COST OF PURCHASE
      cost = shares[index] * price[index];
      total_cost = total_cost + cost;

     // CALCULATE COMMISSION
      commission = calculateCommission(shares[index], cost);
      total_commission = total_commission + commission;

    /* **********************************
    Output
    ********************************** */
     // PRINT DETAILS OF PURCHASE
      printPurchase(shares[index], cost, commission, name[index]);

     // UPDATE FOR TRANSACTION WHILE LOOP
          transaction = anotherTransaction();
        }

     // TOTAL PURCHASES, COST, AND COMMISSION
      printSummary(purchases, total_cost, total_commission);

     cout << "Thank you for using (my name)'s Stockbroker." << endl;

// Return that program executed successfully
    return EXIT_SUCCESS;
}

void printMenu(string name[], double price[], int size)
{
    int index;

    cout << "Here is a menu of stocks you can purchase:" << endl;
    for (index = 0; index < size; index++)
    {
      cout << "Enter " << index + 1 << " for " << name[index] << endl;
    }

    return;
}

int getCompany(string name[], double price[], int size)
{
    int stock;

    do
    {
      // PRINT MENU OF STOCKS
      printMenu(name, price, size);
      cin >> stock;
    } while (stock < 1 || stock > 8);

    return stock;

}

double getShares(string name) 
{
    double shares;
    int index;

    do
    {
      cout << "How many shares of " << name
           <<  " would you like to buy? " << endl;
      cin >> shares;
    }while (shares < 0);

    return shares;
}

double calculateCommission(double shares, double cost)
{
    const double HUNDRED_OR_LESS_COMMISSION = 9.99;
    const double THOUSAND_OR_LESS_COMMISSION = 19.95;
    const double THOUSAND_OR_MORE_COMMISSION = .025;

    double commission;
    int index;

    if(shares < 100)
    {
      commission = HUNDRED_OR_LESS_COMMISSION;
    }
    else
    {
      if(shares >= 1000)
      {
        commission = THOUSAND_OR_MORE_COMMISSION * cost;
      }
      else
      {
        commission = THOUSAND_OR_LESS_COMMISSION;
      }
    }

    return commission;

}

void printPurchase(double shares, double cost,
                    double commission, string name)
{
    int index;

    cout << "The cost of " << shares << " shares of "
         << name << " is $" << cost << "." << endl;
    cout << "The commission paid is $" << commission << endl;

    return;
}

bool anotherTransaction()
{
    char transaction;

    do
    {
      cout << "Would you like to buy another stock (y/Y/n/N)?" << endl;
      cin >> transaction;
        transaction = tolower(transaction);
    }while (!(transaction == 'y' || transaction == 'n'));

    if(transaction == 'y')
    {
      transaction = true;
    }
    else
    {
      transaction = false;
    }

    return transaction;
}

void printSummary(int purchases, double total_cost,
                               double total_commission)
{
    cout << "You have made " << purchases << " purchases." << endl
         << "The total cost is $" << total_cost << "." << endl
         << "The total commission is $" << total_commission << "."
         << endl;

    return;
}

// PROJECT 8
double getPrice (string name)
{
    double price;
    int index;

    do
    {
    cout << "Enter the price of a " << name
         << " share." << endl;
    cin >> price;
    } while (price < 0);

    return price;
}

void getMenu(string name[], double price[], int size)
{
    int index;

    for (index = 0; index < size; index++)
    {
      cout << "Enter the name of company " << index + 1 << endl;
      cin >> name[index];
      price[index] = getPrice (name[index]);
    }

    return;

}

void printOwnedShares(string name[], double shares[], int size)
{
    int index;

    cout << "You currently own: " << endl;
      for (index = 0; index < size; index++)
      {
       cout << shares[index] << " shares of " << name[index] << endl;
      }

    return;
}

and this is a sample run:

Enter the name of company 1
a
Enter the name of company 2
b
Enter the name of company 3
c
Enter the name of company 4
d
Enter the name of company 5
e
Enter the name of company 6
f
Enter the name of company 7
g
Enter the name of company 9
h
Would you like to buy another stock (y/Y/n/N)?
y
Here is a menu of stocks you can purchase:
Enter 1 for a
Enter 2 for b
Enter 3 for c
Enter 4 for d
Enter 5 for e
Enter 6 for f
Enter 7 for g
Enter 8 for h
3
How many shares of c would you like to buy?
100
You currently own:
100.00 shares of a
The cost of 100.00 shares of a is $1000.00.
The commission paid is $19.95
Would you like to buy another stock (y/Y/n/N)?
y
Here is a menu of stocks you can purchase:
Enter 1 for a
Enter 2 for b
Enter 3 for c
Enter 4 for d
Enter 5 for e
Enter 6 for f
Enter 7 for g
Enter 8 for h
7
How many shares of g would you like to buy?
200
You currently own:
200.00 shares of a
0.00 shares of b
The cost of 200.00 shares of a is $2000.00.
The commission paid is $19.95.
Would you like to buy another stock (y/Y/n/N)?
n
You have made 2 purchases.
The total cost is $3000.00.
The total commission is $39.90.
Thank you for using (my name)'s Stockbroker.

What I want is for the "you currenly own:" part to print the correct shares corresponding to the correct name of the company. I think I need an if/else statement instead of the for statement or I am not using it correctly in the first place. I want more than anything to be able to understand this :cry:
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
you need to pass to getMenu by reference because as it is right now that function doesn't do anything for the calling program. change that arguments to be (string& name[], double& price[], int& size)
 
  • #3
ice109 said:
you need to pass to getMenu by reference because as it is right now that function doesn't do anything for the calling program. change that arguments to be (string& name[], double& price[], int& size)
You misunderstand C++ semantics. The original poster's function is correct -- yours doesn't even compile!

In the original poster's prototype, the first argument was declared as string name[], so it is a pointer to a string. When getMenu is called, a copy of the first argument is stored in name -- which is exactly what we want, because the first argument is a pointer to the first memory location of the array of data we want to set.

You attempted to declare name as a pointer to a strong reference, which doesn't make sense, because reference types aren't stored in memory; you cannot point to them!

You could have made the first argument string *&name -- but there is no good reason: you do not need to change the value of the first argument to the function (which is the address in memory of the 0th element of the array). Similarly, there's no reason to pass size by reference, because the function doesn't change its value.
 
  • #4
For the opening poster...

Code:
 // RETURN NUMBER OF SHARES
      if (stock == 1)
        shares[index] = getShares(name[0]);
      else if (stock == 2)
        shares[index] = getShares(name[1]);
      else if (stock == 3)
        shares[index] = getShares(name[2]);
      else if (stock == 4)
        shares[index] = getShares(name[3]);
      else if (stock == 5)
        shares[index] = getShares(name[4]);
      else if (stock == 6)
        shares[index] = getShares(name[5]);
      else if (stock == 7)
        shares[index] = getShares(name[6]);
      else
        shares[index] = getShares(name[7]);
Is there any particular reason why you made this a giant chain of if's? And I'm having trouble finding in your code what index is supposed to be.

I also noticed this line:
Code:
} while (stock < 1 || stock > 8);
which doesn't make any sense to me.

Incidentally, did you bother to tell your compiler to emit warnings? And did you pay attention to them?



Anyways, the problem with your output is that it's not adding the purchased shares to the right stock, right? It seems the obvious thing to do is to ensure that you've got the correct stock, and ensure you're adding those shares to the correct total. Have you looked at those parts of your code? Have you reviewed those parts of your program to make sure you're giving the right commands? Have you inserted debugging statements to ensure that they're behaving the way they're supposed to behave?
 

What are arrays and strings?

Arrays and strings are two types of data structures used in computer science. An array is a collection of data elements of the same data type, stored in contiguous memory locations. A string is a sequence of characters stored as a single data type. In many programming languages, strings are represented as arrays of characters.

Why are arrays and strings important?

Arrays and strings are important because they allow programmers to store and manipulate large amounts of data efficiently. They are widely used in a variety of applications, such as sorting and searching algorithms, data processing, and text manipulation.

How can I improve my understanding of arrays and strings?

To improve your understanding of arrays and strings, you can practice writing code that uses these data structures. You can also read articles and tutorials, watch videos, and attend workshops or classes on the topic. Additionally, it can be helpful to ask questions and seek guidance from more experienced programmers.

What are some common challenges when working with arrays and strings?

Some common challenges when working with arrays and strings include understanding how to access and manipulate specific elements, dealing with memory and performance limitations, and avoiding errors such as out-of-bounds access or null references. It is also important to understand the differences between mutable and immutable strings, and how to properly handle string concatenation.

How can I prepare for a final exam on arrays and strings?

To prepare for a final exam on arrays and strings, it is important to review and practice the basics, such as creating and accessing arrays and strings, as well as more advanced concepts like sorting and searching. You can also create flashcards or study guides to help you memorize important information and practice solving coding problems related to arrays and strings. It can also be helpful to review any notes or assignments from your class or textbook.

Suggested for: Help! Struggling to Grasp Arrays and Strings for Final Exam

Replies
4
Views
1K
Replies
10
Views
2K
Replies
17
Views
1K
Replies
4
Views
1K
Replies
28
Views
1K
Replies
1
Views
682
Replies
3
Views
1K
Back
Top