0

Also, you told me to fix that while loop to a for loop. At the moment it looks like this:

while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}

int counter;

But I'm really struggling to get a grasp on what you mean... Could you possible show me how I should change it? Thank you in advance!

I thought you said earlier that you had gotten rid of this line? Check if you use it. If not, get rid of it.

int counter;

Yep, the line below declares and initializes in one statement, so you're good on the initialization part.

counters[2][2] = {0};

As for turning the for loop into a while loop, look carefully at the above code you've posted (I assume you realize you did not post the entire while loop):

while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}

int counter;

Recall that you have already changed numRows into i everywhere. That means you can use numRows now for something else and you need to put i somewhere in the loop below. Let's look at a for-loop:

for (int variable1 = 0; variable1 < variable2; variable1++)

Let's look at your data:

5
1 0 0
2 0 0
3 0 0
4 1 2
5 1 2

How many times are you going through the loop in this data? That needs to go in the for loop. Look at the for loop layout carefully.

When will you read in that 5 from the data file? Before the for loop or inside the for loop? variable2 clearly needs to have something in it for the loop to make sense.

What should the variable names for variable1 and variable2 be? Look at your code, particularly the matrix index variables.

As for this line:

inFile >> matrix[numRows][0];

decide where it needs to go and whether it still needs to be there. Hint: look at the inner for loop you have already. Can it be changed so you no longer need the line above?

for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}

One last thing. You changed all of your numRows variables from the last thread into i variables in this thread when you knew you were changing from a while loop to a for loop. Be very careful not to get confused between the two versions. There are only a few lines you need to change. I've given you lots of hints. Give it a try. You'll undoubtedly, as JRM mentioned, get some errors and have to play around with the code more. He's correct. It's inevitable and it's not a bad thing. Save an original copy of your code so you can go back to it, then experiment!

0

Great. Everythings running really well. Apart from this counter:

for(i = 0; i < numRows; i++)
{
if (matrix[i][3]==0  && matrix[i][4] ==0) 
{ 
counters[0][0]++; 
}

else
if (matrix[i][3]==0  && matrix[i][4] ==1) 
{ 
counters[0][1]++; 
}

else
if (matrix[i][3]==1  && matrix[i][4] ==0) 
{ 
counters[1][0]++; 
}

else
if (matrix[i][3]==1 && matrix[i][4] ==1)
{
counters[1][1]++;
}
}
printf("\n");
printf("00: %d\n", counters[0][0]);
printf("01: %d\n", counters[0][1] + counters[1][0]);
printf("11: %d\n", counters[1][1]);
printf("\n");

On the fourth to last line, I want to add 01 totals and 10 totals together, because they are the same. But putting the code on this line like I have doesn't seem to work. It doesn't add up right. Do you know why?

0

Let me see if I follow. You have been able to print out the four counters (00, 01, 10, 11) separately and that is successful and is accurate, but when you try to combine two counters you are unsuccessful?

0

That's the C stuff. I don't know too much about it,but you may be asking it to do something it knows nothing about. That may be one of those scenarios where function overloading is needed.
Don't go there yet, not necessary!

declare a variable to dump the sum into.
print the variable.

0

Also try putting the sum of the matrices in parenthesis in he argument. That may keep away the confusion. Then it is seen as one item.

0

Yeah thats right it works fine without adding them together, but just doesnt when i do.

The correct scores are not added up.

I've tried dumping them in variables and then adding the variables up instead, but I couldn't get it to work.

Any other ideas?

0

Don't worry guys I solved it. I just did this:

else
if (matrix[3]==0 && matrix[4] ==1 || matrix[3]==1 && matrix[4] ==0)
{
counters[0][1]++;
}

0

My last question I think :)

I need to print all of my work to an output file called 'output.txt'

Could anybody show me how to do this please?

Thank you :)

0

Andy I typed your code into a program and used your headers and it worked for me. I think the code you posted used printf correctly. Can't see anything wrong with it. Particularly if you have tried JRM's suggestion and dumped the two variables into another variable and then did a printf on the variable, it makes even less sense that it would fail for you when you add two variables, stick that result in a third variable, and print the third variable unsuccessfully, but be able to print the first two correctly. That doesn't make sense to me. Please try again to list the variables separately without combining and verifying that those results are correct. Below is the code I tried on my computer:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
using namespace std;
int main ()
{
    int counters[2][2] = {0};
    counters[0][0] = 9;
    counters[0][1] = 8;
    counters[1][0] = 3;
    counters[1][1] = 4;    

    printf("\n");
    printf("00: %d\n", counters[0][0]);
    printf("01: %d\n", counters[0][1] + counters[1][0]);
    printf("11: %d\n", counters[1][1]);
    printf("\n");

    return 0;
}

Let me verify that you are using a C++ compiler, not a C compiler, correct? The above program compiled and ran fine for me with a C++ compiler, though it gave me the warnings about the outdated headers. You mentioned that you are stuck with these headers, but it should still work. Run it on your own machine. If you get the right results with it but bad results with YOUR program, then the problem is elsewhere.

You've made a lot of revisions since you last posted your full program. You've posted snippets, but maybe if you posted the full program again we could look at it and verify that changes you made recently have not inadvertently caused errors to occur in places that worked before.

0

Don't worry guys I solved it. I just did this:

else
if (matrix[3]==0 && matrix[4] ==1 || matrix[3]==1 && matrix[4] ==0)
{
counters[0][1]++;
}

Guess we overlapped while posting. If you solved it, disregard my last post.

0

Haha, yes we did.

I did post one last question though :)

0

My last question I think :)

I need to print all of my work to an output file called 'output.txt'

Could anybody show me how to do this please?

Thank you :)

Might want to start a new thread for that since it's sort of a new topic and maybe you'll get some new commenters. I'd make sure everything is working without it first though and that you understand it all and can explain it to people since they may not have read this one. You may get questions about the old headers if you don't explain that you're on an old system that needs them. Are you keeping the output to the screen and adding the output to the text file or are you replacing it?

You'll likely use an ofstream from the fstream library:
http://www.cplusplus.com/reference/iostream/ofstream/

Here's a little program to give you the idea of how to use it:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <fstream.h>
using namespace std;
int main ()
{
    ofstream output;
    output.open ("output.txt");
    output << "Hello World!\n";
    output.close ();

    return 0;
}

It uses your old style ".h" header that you're apparently stuck with.
If you start a new thread, you may want to mark the old ones as "Solved" so people don't post in multiple threads.

0

Thanks for that. I did completely forget though, I wanted to track 2 individuals and record the numbers of 00's 01's and 11's too.

So I set up a very similar counter as the one I did before. It isn't working though for some reason. So I put...

Enter the first individual:
> 9

Enter the second individual:
>10

and I want it to go:

ind9
00 = 5
01 = 20
11 = 18

ind 10
00 = 5
01 = 20
11 = 18

So I declared another counter globally (indCounter[2][2] = {0})

My code is this:

int ind1, ind2;
int indIndex1, indIndex2;

   cout << "Enter the first individual: ";
   cin >> ind1;
   cout << "\n";
   cout << "Enter the second individual: ";
   cin >> ind2;
   cout << "\n";  

indIndex1 = ind1 - 1;
indIndex2 = ind2 - 1;

   for (i = 0; i < numRows; i++){
   
   if (matrix[indIndex1][3] == 0 && matrix[indIndex1][4] == 0)
   {
   indCounters[0][0]++;
   }

   else
   if (matrix[indIndex1][3] == 0 && matrix[indIndex1][4] == 1 || matrix[indIndex1][3] == 1 && matrix[indIndex1][4] == 0)
   {
   indCounters[0][1]++;
   }

   else
   if (matrix[indIndex1][3] == 1 && matrix[indIndex1][4] == 1)
   {
   indCounters[1][1]++;
   }  
   }

I then want to repeat this for ind2

I did ind1 -1 and ind2 -2 because the first row starts at 0 right? So individual 9 wouldnt be on row 9, they would be on row 8 ? (9 - 1)

My program will not compile for this loop. It starts and I get half way through, it goes to run the loop and then aborts?

Any ideas?

0

So you are running it 1000 times now? Or just once and keeping track of two individuals? If the former, I think you need to post the whole program. The below code looks reasonable to me but I think I'd need to see the whole updated program to comment further:

int ind1, ind2;
int indIndex1, indIndex2;

   cout << "Enter the first individual: ";
   cin >> ind1;
   cout << "\n";
   cout << "Enter the second individual: ";
   cin >> ind2;
   cout << "\n";  

indIndex1 = ind1 - 1;
indIndex2 = ind2 - 1;
0

Ahh, I had to put this:

indIndex1 = ind1 - 1;
indIndex2 = ind2 - 1;

After I declared what ind1 and 2 was. That compiled.

However, when I come to print the results, when I run it once, it goes:

00 = 0
01 = 10
11 = 0

And then when I ran it 9 times, it gave me:

00 = 0
01 = 90
11 = 0

Which is obviously wrong.

Should I post you the whole code?

0

Sorry, I posted it, did you get it? I don't want to leave it up as I don't want it to get seen.

For the first option, use 0.4.

For the second, Use a repeat from 1 - 9, it doesnt work properly with higher numbers for some reason...

0

Sorry, I posted it, did you get it? I don't want to leave it up as I don't want it to get seen.

For the first option, use 0.4.

For the second, Use a repeat from 1 - 9, it doesnt work properly with higher numbers for some reason...

Yeah, I got it and saved it. Looking at it now. Will respond within 15 minutes.

0

I think the site wants you to keep up what you post so if people later type in a query and come to this thread they can learn from it, which they can't if the code is taken down. It's part of the "Keep It Public" rule. That aside, my comments...

It looks like you still have the while loop instead of the for loop. See earlier posts on how to convert.

So in these lines:

printf("Please enter the desired number of repeats: ");
scanf("%d", &norepeats);

If the user types in 12, file is read in and all the calculations are repeated 12 times? If so, you have at least one problem with your loop control. You have this line:

numRows = 0;

before this line:

while (repeats != norepeats){

Thus if you have 10 lines of data in your input file, the first time through numRows will be correct, but the second time around, numRows will start at 10, not 0. The third time it'll start at 20, then30, etc., since you're never resetting it to 0.

numRows = 0;

needs to be inside the above while loop, not before since you wnat to reset it to 0 each time.

You also appear to be closing your outer while loop far too early:

repeats++;
}

Everything after this is only done once. Also, it seems to me that asking for the two individuals needs to occur BEFORE the outer while loop starts, not after. I'd stick all of this:

int ind1, ind2;
int indIndex1, indIndex2;

cout << "Please choose your first individual from the genealogy: ";
cin >> ind1;
cout << "\n";
cout << "Please choose your second individual from the genealogy: ";
cin >> ind2;
cout << "\n";  

indIndex1 = ind1 - 1;
indIndex2 = ind2 - 1;

above the outer while loop, and I would not end the outer while loop so early. I think you are not repeating code that you need to be.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.