Array problem

Pages: 12
Please tell me that how to store values in an array? E.g

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>

#include <iostream>

using namespace std;

int main()
{
    int x;
    int z;
    int y[z];


    for(x=1 ; x<=10; x++)
    {
        cout << "Enter the number of pancakes eaten by "<< x << "." << endl;
        cin >> z;
    }



    cout << "Person    -    Pancakes\n";



    for (x=1 ;x<=10; x++)
    {
        cout << x << "    -    "<< y << endl;
    }
return 0;


I know this program is wrong. But I cant seem to understand. How can I use the values of the array which is 'y' in the list below?

Please just change this program to how it is supposed to be instead of explaining everything. I understand it better this way.
You would access the element in your for loop with y[x]. There are alternatives though:
1
2
3
*(y+x);
*(x+y);
x[y];


http://www.cplusplus.com/doc/tutorial/arrays/
Please just fix the program instead of explain or giving me links. I've read this whole article about arrays already.

I've been trying to make this program for 4 days now and all you guys do is give short explainations and give me links.

Please just fix the program. They way it is supposed to be.
well, you obviously didn't read the section "Accessing the values of an array" in giblit's post.
I read it. It only tell how to get a value from an array. But all I want to do it is store the values in an array and then use them in the list below it ( Referring to my program )
talhabhatti5 wrote:
It only tell how to get a value from an array.

It also tells you how to store values in the array.
Tutorial wrote:
For example, the following statement stores the value 75 in the third element of foo:

 
foo[2] = 75;


So if you want to assign the value z to the element at index x in the array y you can do y[x] = z;
Last edited on

Try this, hopefully this will give you a little more clarity. If you have any questions about the code just send me a message. However, your comment "Please just fix the program. They way it is supposed to be" I would think would put people off helping you.

The guys on here were doing it the right way and merely pointing you in the the right direction so that you would discover your mistakes, and hopefully by discovering them yourself you would learn from it.

If people simply code for you, you will learn nothing.

Anyway.. here is the code, hope you learn from it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

const
	int NUM_NAMES = 5;			// here for simplicity I am assuming your storing 5 names and how many pancakes they ate.

// structure for our array
struct users
{
	char Name[50];			// max 50 characters for name.
	int Pancakes;			// number he/she ate

};  struct users Names[NUM_NAMES];		

int main()
{

	// We will loop 5 times updating the index of our array so that
	// each name is stored in the correct position.
	for (int count = 0; count < NUM_NAMES; count++) {
		cout << "Enter Name (" << count + 1 << "): ";
		cin >> Names[count].Name;
		cout << "How many pancakes for " << Names[count].Name << "? ";
		cin >> Names[count].Pancakes;
	}
	
	// we now have the names and pancakes stored, lets show them.
	for (int count = 0; count < NUM_NAMES; count++) {
		cout << "Name: " << Names[count].Name << " has eaten " << Names[count].Pancakes << "." << endl;
	}

	return 0;
}


Hope this helps, happy coding :)
Last edited on
can you help me..

i read the struct codes tutorial but.. i dont understand..

what its function..?

please can you explain it to me.. thank you very much..

I think you may have hijacked some other question with yours :)

Anyway, a structure is a way of grouping data elements "members" under one tidy name (i.e. arrays, integers etc).

I have just looked at the structures section you referred to and its very well written and explains structures well - for that reason I am not going to repeat any of it here.

Its normal that it sometimes doesn't become clear when reading it so I would suggest reading it over and over until you do - try some examples in a IDE and experiment.



closed account (iAk3T05o)
Softrix what are you doing? Using a char array to get a string and what is struct user Names[5];
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//#includes
int main ()
{
int pancakes [5]; //create an array that can store 5 pancakes (0 - 4)
int person = 1; //initialize your variables, variable for person
for (person; person < 5; ++person)
{
cout << "How many pancakes did person" << person << " eat: ";
cin >> pancakes [person]; //the [person] increases by 1 to store pancakes data everytime the loop runs
}


for (person; person < 5; ++person)
{
cout << "Person: " << person << "ate " << pancakes [person] << " pancake(s) "; //access all values stored in pancakes with [person] and print it
}

return 0;
} 

Is it ok?
Last edited on


lol, I am wondering that myself after reading your reply and looking at the original post... for some odd reason (who knows why) I had it in my head he wanted to store names alongside the number of pancakes that person ate... its clearly one of those days....

oh well he got more that what he asked for.. cant complain at that.. now i need a lie down ^^

Been a long day :)


Nathan2222: You program really helped me. So thank you so much for that.

And thank you everyone else too.

Can't believe I finally made it. I've been trying to make this program for a week now and I was getting really frustrated. I feel like I've won an Oscar ! :D

Once again. Thank you so much everyone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>

using namespace std;

int main()
{
    int person;
    int pancakes[11];


    for (person =1; person <= 10; person++)
    {
        cout << "Enter the number of pancakes eaten by person " << person << endl;
        cin >> pancakes [person];
    }

    cout << "Person    -    Pancakes\n";



    for (person=1 ;person <= 10; person++)
    {
        cout << person << "    -    "<< pancakes [person] << endl;
    }

    return 0;
}

Last edited on

lol mine was a little over the top from what you wanted because for some reason I had it in my head you wanted to store names and the number of pancakes they ate... but obviously not and realised once Nathan pointed it out lol.

I'm sure my code will come in handy for something as you work on new projects.

Glad you got it sorted out and working though :)

I tried your program too but I was a lil complex for a beginner like me :)

I actually started doing the practice exercises on the site. Which is really helping me alot. I read it first. At first I try to make it myself, then watch the videos on youtube on the related topic and even then if I couldn't understand I post it here.

Am I going on the right path? Please suggest what else shoudl i do to make my coding better. Will really appreciate it :)
Read lots and lots of tutorials :). And yeah you are going on the right path I guess. You might want to start a project of your own though soon. Try to apply whatever you have learnt.
Last edited on
closed account (iAk3T05o)
You are going on the right path and I had way more serious problems with arrays and for loops.

I started 4 months ago and i'm almost done with 3 books. Just make sure you finish the books and practice, practice, practice (don't forget to have fun).
My plan is once i finish the books (maybe by the end of this month), i make a huge program that uses all i've learnt and ask for code reviews.
Last edited on
I also started I guess 2 months ago. But I've been quite lazy and because of that I forget stuff. And then I have to watch the videos again.

Btw. I'm not learning from a book. Should I? Because I find it easier to learn from videos.

In this same program I'm also required to finf the max and min number of pancakes eaten by a person. I was able to make it but the code is not giving the correct value of max. Min is coming out just fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <iostream>

using namespace std;

int main()
{
    int person;
    int pancakes[11];
    int MAX;
    int MIN;


    for (person =1; person <= 10; person++)
    {
        cout << "Enter the number of pancakes eaten by person " << person << endl;
        cin >> pancakes [person];
    }


    cout << "Person    -    Pancakes\n";



    for (person=1 ;person <= 10; person++)
    {
        cout << person << "    -    "<< pancakes [person] << endl;
    }
    
    MAX = pancakes[0];
    MIN = pancakes[0];


    for (person =1; person <= 10; person++)
    {
        if (pancakes[1] > MAX)
        {
            MAX = pancakes[1];
        }

    }

    for (person =1; person <= 10; person++)
    {
        if (pancakes[1] < MIN)
        {
            MIN = pancakes[1];
        }

    }

    cout << "Maximum number of pancakes eaten by a person are "<< MAX << endl;
    cout << "Maximum number of pancakes eaten by a person are "<< MIN << endl;





    return 0;
}

Last edited on
closed account (iAk3T05o)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// #includes

int main()
{
 int pancakes [10];

 int person = 1;
 int min = 0;
 int max = 0; 

 for (person; person < 10; ++person)
 {
  cout << "Person " << person << " ate how many pancakes: ";
  cin >> pancakes [person];

  min = pancakes [0]; // min is equal to the no. of pancakes eaten by the 1st person

  max = pancakes [0]; // max is equal to the no. of pancakes eaten by the 1st person

   for (int i = 0; i < 10; ++i)
   {
   
    if (max < pancakes[i]) // if the no. of pancakes eaten by the 1st person (max) is less than the no. of pancakes eaten by the next person . .  
    {
     max = pancakes [i]; // . . . max is equal to that higher no.
    }

    else if (min > pancakes[i]) // else if the no. of pancakes eaten by the 1st person (min) is greater than the no. of pancakes eaten by next person . . . 
    {
     min = pancakes [i]; // . . . min is equal to that lower no.
    }

   }

  }

for (person; person < 10; ++person)
{
 cout << "Person " << person << " ate " << pancakes [person] << " pancakes" << endl;
}

 cout <<  "Maximum number of pancakes eaten is: " << max << endl;

 cout << "Minimum number of pancakes eaten is: " << min;

 return 0;
}

Is it ok?
I prefer books because i don't like pausing/playing when i'm learning except it's 3d modelling.
Last edited on
I don't know why but your program wasn't working. But I understood the concept from it. I made it. thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include <iostream>

using namespace std;

int main()
{
    int person;
    int pancakes[11];
    int MAX;
    int MIN;


    for (person=1; person <= 10; person++)
    {
        cout << "Enter the number of pancakes eaten by person " << person << endl;
        cin >> pancakes [person];
    }


    cout << "Person    -    Pancakes\n";



    for (person=1;person <= 10; person++)
    {
        cout << person << "    -    "<< pancakes [person] << endl;
    }


    MAX = pancakes[0];
    MIN = pancakes[0];


    for (int i=1; i <= 10; i++)
    {
        if (pancakes[i] > MAX)
        {
            MAX = pancakes[i];
        }

        else if (pancakes[i] < MIN)
        {
            MIN = pancakes[i];
        }

    }

    cout << "Maximum number of pancakes eaten by a person are "<< MAX << endl;
    cout << "Minimum number of pancakes eaten by a person are "<< MIN << endl;





    return 0;
}
closed account (iAk3T05o)
That's weird because i just ran it.
I realised in the code i posted, person = 1 but int i = 0 so it doesn't check it correctly.
Does my code show a large negative number?
Pages: 12