Is there any easier way to look at arrays? Basically how to manipulate them, perhaps change them into strings if determining for example, which flavor (Hot, Medium, or Mild) had the most likes. (It'll output the string name, not the data)
I want to master C++, and arrays seem to be confusing me a little bit. Any help? I would greatly appreciate it! Feel free if you'd like to refer me to other topics on the forum or sites I could read up on. Thanks!
Here's my code example of what I can do so far at least
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
usingnamespace std;
string Flavors[3] = {"Hot" , "Medium" , "Mild"}
int Data[3][3][3];
int main()
{
for (int i = 0; i < 3; i++)
{
cout << "How many people like " << Flavors[i];
cin >> Data[i];
}
for (int i = 0; i < 3; i++)
{
cout << Data[i] << " people like " << Flavors//and... I don't know.
//Come to think of it, how would I access the second array? Or the third?
}
}
That code doesn't compile.
It also doesn't make a lot of sense.
Most things about the code are focussed on the idea of three.
But what is int Data[3][3][3];? That is a 3-D array, Think of it like a cube, It has 27 elements. Why so many? That's 24 more than you need, as well as not matching the syntax of how you want to use it.
Line 4 is incomplete, the semicolon is missing.
Line 15 is incomplete. It is missing the array subscript, I'd guess it is missing the output of a newline, and it is definitely missing a semicolon.
@Chervil
I know it doesn't compile, I was giving an example of an incomplete program to show my understanding of arrays. (I clearly need reassuring of how to use them)
@Kemort
Thank you, how would you compare which one is the LARGEST and which one is the SMALLEST perhaps? Like saying which flavor has the largest value.
// Search for most popular flavor
int most = Data[0];
int most_index = 0;
for (int i=0; i<3; i++)
{ if (Data[i] > most)
{ most = Data[i];
most_index = i;
}
}
cout << "The most popular flavor is: " << Flavors[most_index] << endl;
cout << " with " << most << " people liking that flavor" << endl;
}
Do the same thing for least, just change the comparison at line 25.
A slight simplification because if you know the index of the least (or most) then by calling up the data at that index you know both the flavor and number without separately storing it.
@AbstractionAnon, @kemort
Ah I see, that seems simple enough. Thanks! I don't know why I couldn't come up with it. You're using index, is that actually a "thing" in the compiler? Or is it just a commonly used name for a variable? Also, in the outputs it outputs the index, or one specific number. Is it to just match the data values with the corresponding strings? Ex: String Hot is Flavors[0], integer value 56 is Data[0].
@AbstractionAnon
Are you making [code] int most = Data[0] [code] to make the integer variable equal to the first number in the array, and comparing? Overall though, makes sense to me, thanks
@kemort
I like the way you coded your part, simple and to the point. (That's how I like to code) thank you for your reply
You're using index, is that actually a "thing" in the compiler? Or is it just a commonly used name for a variable?
Its just the name he chose for the variable, not a "thing" in the compiler.
Are you making [code] int most = Data[0] [code] to make the integer variable equal to the first number in the array, and comparing? Overall though, makes sense to me, thanks
Yes. The algorithm is as follows:
(1) Start with most being the first number
(2) Traverse through the array (using for-loop), and every time you come across a number larger than most, make that the new most.
(3) Save the index of this new most in another variable.