-1
#include <iostream>

using namespace std;

const int MAXACCOUNTS =8;

int count1 = 1;
bool True = true;

int main()

{

int AccountNumber[MAXACCOUNTS] = {1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342};
double Balance[MAXACCOUNTS] = {4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44};
int DaysSinceDebited[MAXACCOUNTS] = {20, 35, 2, 14, 5, 360, 1, 45};

while (count1 != MAXACCOUNTS)
{
    if (Balance[count1] > 10000 && DaysSinceDebited[count1] > 30)
    {
        cout << AccountNumber[count1] << "  ";
        cout << Balance[count1] << "    ";
        cout << Balance[count1] / 100 * 3 << endl;
    }
    else if (True = true)
    {
        cout << AccountNumber[count1] << "  ";
        cout << Balance[count1] << "    ";
        cout << Balance[count1] / 100 * 3 << endl;
    }
    count1 ++;
}
return 0;
}

Hi Guys

Ive just started c++ and i am stuck. I am practicing for an exam and one of the requirements is to make this program do the same using structs. Im sure its simple can someone help out with some explanation?

  • 1
    What is the question? Please take [the tour](https://stackoverflow.com/tour) and read [the help page](https://stackoverflow.com/help). Welcome to SO. – Ron Feb 06 '18 at 14:26
  • `if (True = true)` should probably be `if (True == true)`. – Jarod42 Feb 06 '18 at 14:31
  • 1
    Basically: Define a struct with the appropriate members, replace your three arrays with one array of said struct, then you fix the syntax errors, then you're done. – molbdnilo Feb 06 '18 at 14:31
  • 1
    Off-topic questions that your teacher might ask and that you might want to prepare answers for: What is the conditional doing? Why aren't you using a for loop? Why is there a variable called "True"? Why do you start indexing at `1`? Why are there global variables? – molbdnilo Feb 06 '18 at 14:34
  • What are the requirements when using an array? (What is it supposed to do?) – Thomas Matthews Feb 06 '18 at 15:09
  • Just like in real life, what you think is `True` can potentially be `false` =) – Ivan Aksamentov - Drop Feb 06 '18 at 15:15

3 Answers3

-1

Since it's a homework, I will not feed you with the answer, but will instead try to help you understand how to complete that homework yourself.

  1. You are using three arrays of the same size.

  2. As you probably know by now, a struct is something you can create to store multiple data

  3. The question doesn't seem to specify that you must not use an array, so my guess would be that you can use one with some data in it ;)

I hope these clues help you get on track.


here's a nice read on structs and how to use them

here's an other one on arrays

Sirmyself
  • 1,464
  • 1
  • 14
  • 29
  • Sorry i should of been more specific. – Matt oakley Feb 06 '18 at 14:31
  • Change your program so that it no longer uses separate arrays, but instead declares a struct called Account for the information for each of the bank accounts, and declares instead an array of Accounts, initialised with the same information. Modify your loop so that it uses the array of structs rather than the separate arrays. You should produce the same display, because the information is the same. It should also work out which account generated the highest amount of interest and display the relevant account number. – Matt oakley Feb 06 '18 at 14:32
  • *Modify your loop so that it uses the **array of structs** rather than the separate arrays.* – Sirmyself Feb 06 '18 at 14:33
  • Your requirement is quite clear, you must create an array of structs and iterate through that array to get the same display than before – Sirmyself Feb 06 '18 at 14:35
  • Sorry - This not homework. I was just revising past exam questions. – Matt oakley Feb 06 '18 at 18:44
  • @matt-oakley Even not for a homework, it remains learning and school work. S-O rules are quite clear about that matter : try to make asker learn more than giving him the answer. Other questions on this site probably answered the question, so a little bit of search would give you the answer you are looking for. – Sirmyself Feb 07 '18 at 19:12
  • For the downvoters : please remove those votes since I am just abiding the rules of the site by giving general hints to the solution. Or at least, specify why you downvoted. – Sirmyself Feb 07 '18 at 19:13
  • https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Sirmyself Feb 07 '18 at 19:16
-1

In C++, arrays and structs are the aggregate types. You may think of them as of "boxes" where you can put "stuff". Into array "box" you can put things of the same kind and refer to these things by their index. Into struct "box" you can put multiple things, potentially of different kinds, and you can give them names to refer to them later.

The trick is that you can nest "boxes" one inside another: for example, you can put array "boxes" into struct "boxes" and struct "boxes" into array "boxes". The exercise is likely about the order of nesting.

Here is a little illustration (pseudo-C++):

Struct of Arrays (SoA)

// Struct of Arrays (SoA)
// Put things into arrays, then put arrays into a struct
// (Like in your code, but arrays are wrapped into a struct)
struct Entries {
    int AccountNumber[MAXACCOUNTS];
    double Balance[MAXACCOUNTS];
    int DaysSinceDebited[MAXACCOUNTS];
};

Entries entries;

// Read a "thing" from SoA
ith_account_number = entries.AccountNumber[i];

// Write a "thing" into SoA
entries.AccountNumber[i] = ith_account_number;

Array of Structs (AoS)

// Array of Structs (AoS)
// Put things into a struct, then put structs into an array
struct Entry {
    int AccountNumber;
    double Balance;
    int DaysSinceDebited;
};

Entry entries[MAXACCOUNTS];

// Read a "thing" from AoS:
ith_account_number = entries[i].AccountNumber;

// Write a "thing" into AoS:
entries[i].AccountNumber = ith_account_number;

The choice of the data aggregation pattern have profound impact on memory layout of data, performance characteristics and code style. One may say, that AoS looks more like "object-oriented" approach (think Java classes), while SoA more like "array-based" programming (think FORTRAN or NumPy).

I hope having this little helper you can solve your exercise easily. Indeed, the only thing different is how you access entries (for reading and writing).

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
-3

Create a struct with AccountNumber, Balance and DaysSinceDebited as it's member variables.

Your stuct should look something like :

struct Account {
    int AccountNumber;
    double Balance;
    int DaysSinceDebited;
}

Then create an Array of Account with the number of elements required and then initialize each element by passing the necessary Data.

For example:

AccountDatabase Account[6];
AccountDatabase[1].AccountNumber = 1001;
AccountDatabase[1].Balance= 4254.40;
AccountDatabase[1].DaysSinceDebited = 20;

and so on for every element.

For the places where you are extracting data from the struct use the following syntax:

For example to read the Account number stored in the first element of AccountDatabase,

AccountDatabase[1].AccountNumber

Therefore, your cout << AccountNumber[count1] << " "; becomes something like

cout << AccountDatabase[count1].AccountNumber << "  ";

and for your Balance it is

cout << AccountDatabase[count1].Balance<< "  ";
L4TTiCe
  • 20
  • 5