Column Arrays + Multiplication..

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
#include <iostream>
#include <fstream>
using namespace std;

ifstream datain ("Walnut_High_School");

int main(){
	int count = 0, player_number[30], weight[30];

	while(true)
	{
		if (datain.fail())
			break;
		datain >> player_number[count] >> weight[count];
		count++;
	}

	for(int i=0; i<20; i++)
	{
		cout << player_number[i] << "\t" << weight[i] <<endl;
	}

	cout << "\n" << "Amount of players: " <<--count<<endl; // correct order of numbers

	int smallest = 5000000, largest = 0; 

	for (int i=0; i<count ;i++)
	{
		if(player_number[i] < smallest)
			smallest = player_number[i];
		if(player_number[i] > largest)
			largest = player_number[i];
	}

	system("PAUSE");
	return 0;
}


I'm trying to remake my code to read 2 columns of a text file and multiply each number and make a third array. For starters, what lines can I remove from this?
Last edited on
Anyone want to help with this? I have a final exam today :(
It's the exact same question as before, and you double posted.
I changed the other topic to "marked as solved" sorry about that. Still no one has replied to any of my posts.
Code that I found online, not sure how to read in from text file however.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
const int n=6;

    int a[n] = {2, 4, 6, 3, 2, 5};
    int b[n] = {3, 2, 1, 4, 2, 3};
    int c[n] = {0};

    for (int h = 0; h < n; ++h)
        for (int g = h; g < n; ++g)
            c[h] += a[h] * b[g];

    for (int i = 0; i < n; ++i)
        std::cout << c[i] << '\n';
}
I think in an earlier post I may have given some not very good advice, which may be why you have this code which contains errors. (It is wrong because the fail() test is out of sequence).
1
2
3
4
5
6
7
    while (true)
    {
        if (datain.fail())
	    break;
        datain >> player_number[count] >> weight[count];
        count++;
    }

Let me suggest a better way, which is to put the input operation inside the condition at the top of the loop.

That code would be better written like this:
1
2
3
4
    while (datain >> player_number[count] >> weight[count])
    {   
        count++;
    }

Now the line count++ is only reached after we are sure something was definitely read from the file. Thus it gives an accurate count of the number of rows stored in the array.

But we can do better than that. What if the file contains more data than will fit in the arrays? That would cause an error, so it should be checked too.

Now the code will look like this (with a few other changes too).
1
2
3
4
5
6
7
8
9
    const int SIZE = 30;
    int player_number[SIZE];
    int weight[SIZE];   
    int count = 0;

    while ((count < SIZE)  &&  datain >> player_number[count] >> weight[count])
    {   
        count++;
    }

Notice the use of the defined constant SIZE. That makes it easy to keep things consistent, if for example you wanted to make the arrays bigger, it means only a single change to the code.
@Chervil thanks for the tips, and alterations to my code. Appreciate it. But I'm trying to get this code to the point where it multiplies the data entered and sends it to a third array.

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>
#include <fstream>
using namespace std;

ifstream datain ("Walnut_High_School");

int main(){
             const int SIZE = 30;
             int player_number[SIZE];
             int weight[SIZE];   
             int count = 0;

    while ((count < SIZE)  &&  datain >> player_number[count] >> weight[count])
    {   
        count++;
    }

	for(int i=0; i<30; i++)
	{
		cout << player_number[i] << "\t" << weight[i] <<endl;
	}

	 for (int  = ;  < ; ++) // not sure what to enter here
            count += player_number[SIZE] * weight[SIZE]; // not sure if right, how can I print to third array?

	}

	system("PAUSE");
	return 0;
}
Last edited on
multiplys the data entered and sends it to a third array.

Well that doesn't sound too difficult. By now you know how to declare an array. You know how to loop through an array. You know how to store a value in a particular array location. I assume you know how to multiply two numbers.

I guess I'm puzzled as to what is holding you back.

Well I'm also puzzled as to why you want to do this. It sounds like you want to multiply the player number by their weight? That just sounds like an strange thing to be doing.
Old code that I turned in as an assignment but I need to know how to multiply the data and send it a third array.. it WILL be on the final exam that I have in a couple of hours... what's above, did I do that correctly?
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
#include <iostream>
#include <fstream>
using namespace std;

ifstream datain ("Walnut_High_School");

int main(){
             const int SIZE = 30;
             int player_number[SIZE];
             int weight[SIZE];   
             int count = 0;

    while ((count < SIZE)  &&  datain >> player_number[count] >> weight[count])
    {   
        count++;
    }

	for(int i=0; i<30; i++)
	{
		cout << player_number[i] << "\t" << weight[i] <<endl;
	}

	 for (int  = i;  i<30 ; i++) // not sure what to enter here
            count += player_number[SIZE] * weight[SIZE]; // not sure if right, how can I print to third array?
cout<<"Multiplication: "<<count<<endl;

	}

	system("PAUSE");
	return 0;
}
Old code that I turned in as an assignment

OK. But it is completely confusing to read the code now. We always tell people to use meaningful variable names. If you re-use old code, make sure that any names are changed to reflect the new purpose. Otherwise the code becomes impossible to understand.


At lines 9 and 10 you declare two arrays:
9
10
    int player_number[SIZE];
    int weight[SIZE];

So that would be an ideal place to declare any other array you need.

At line 18
 
    for(int i=0; i<30; i++)

You should not be using i<30. Firstly because you already have a constant named SIZE which should be used instead of 30. But more important. Neither of those would be correct here. You want the loop to repeat the same number of times as the number of rows which were read from the file. Conveniently you already have a variable which holds exactly that piece of information. It is the variable named count. Remember it was set to zero at the start, and you added 1 to it each time a row was read from the file. Make use of it.

The same applies at line 23.

Now you know what count does then it is pretty much out-of-place to start changing its value at line 24. Instead you should store the result in the new array you just declared at line 11.
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 <fstream>
using namespace std;

ifstream datain ("multiplythearrays.txt");

int main(){
             const int SIZE = 30;
             int first[SIZE];
             int second[SIZE]; 
             int third[SIZE];
             int count = 0;

    while ((count < SIZE)  &&  datain >> first[count] >> second[count])
    {   
        count++;
    }

	for(int i=0; i < count; i++)
	{
		cout << first[i] << "\t" << second[i] <<endl;
	}

	 for (int  = i;  i < count ; i++) 
            third[SIZE] += first[SIZE] * second[SIZE]  
          cout<<"Multiplication: "<<count<<endl;

	}

	system("PAUSE");
	return 0;
}


I'm lost on how to multiply if it's not like this: third[SIZE] += first[SIZE] * second[SIZE]
As for the printing out, I'm lost, and storing it in.
Last edited on
Test in 30 minutes, not sure how to print out or the multiplcation..Multiplication is wrong apparently..
Well done for correctly declaring the new array.

I don't think you understood what I said about count in the last post.

I don't know why you chose "+="
Read about the different operators here:
http://www.cplusplus.com/doc/tutorial/operators/


Last edited on
"Conveniently you already have a variable which holds exactly that piece of information. It is the variable named count. Remember it was set to zero at the start, and you added 1 to it each time a row was read from the file. Make use of it."

I placed count in i < count.
third[SIZE] = first[SIZE] * second[SIZE]
@chervil, guess you gave up on me lol
Not really. But the amount of explanation needed will take longer than the time you said you had available. I suggest you take your time and go through things slowly and carefully. Make sure you understand each step. Jut trying random changes isn't going to get results.

This loop looks reasonable:
1
2
3
4
    for (int i=0; i < count; i++)
    {
        cout << first[i] << "\t" << second[i] <<endl;
    }

The other loop starting at line 24 should also use i as the subscript.

At line 35
 
    third[SIZE] += first[SIZE] * second[SIZE]

Remember subscripts start from zero. if SIZE was equal to four, valid elements would be first[0], first[1], first[2] and first[3]. Those would be the four array elements.

So which element is accessed as first[SIZE]? Well in this example, I chose SIZE=4 so that becomes first[4]. but that isn't any of the four possible elements. It is outside the array. That can cause undefined behaviour including incorrect results and program crashes.

And what about the += operator?
a += b * c
is a shorter way of saying
a = a + b* c
that is, take the existing value of a and add to it the result of b * c.
But that isn't what is needed. All that is required is a simple =
a = b * c
in order to store the result.


Topic archived. No new replies allowed.