### Midterm reviewin'

So, I hope this post topic will be active to help me prep for Thursday's midterm...I am fully aware that this does not require responses, but they would really really be appreciated!

So!

I have this function that I had to write the body for, it returns true if
A[i,j] == A[j,i] for all 0 <= i,j <= N

my attempt at the solution:
 ``123456789101112`` ``````bool symmetric (double A[N][N]){ for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(A[i,j] == A[j,i]) return (i == N && j == N);//A friend said this wasn't quite right else return false; } { }``````

So for this problem, I had to check each element at most once and see if flipped, they'd be the same pretty much. I thought I had it but the return didn't seem quite right and I wanted to know what I needed to think to make it right...
Last edited on
That is totally wrong... You are terminating your loops early by using the return statement. You will only be comparing 1 value of i and j before the function returns.

Also, not sure if you really meant 0 <= i, j <= N (in which case there should be N+1 elements).
Also, line 6 is incorrect. You can't use the , like that in arrays and get the result you are expecting.
 ``1234567891011`` ``````const int N = 100 ; bool symmetric( const double A[N][N] ) // const added { for( int i = 0 ; i < N ; ++i ) for( int j = i+1 ; j < N ; ++j ) // need to check only for every j > i //if( A[i,j] != A[j,i] ) return false ; // if not equal, no need to check anything more if( A[i][j] != A[j][i] ) return false ; // thanks, firedraco, Zhuge return true ; // ok, symmetric }``````
Last edited on
Thank you so much for your reply, I am serious with that condition:
0<= i,j <= N

Im also not sure why you are saying my loops are terminating early?

This is the review question:

Define a function bool symmetric(double A[N][N]) which returns true if
A[i,j] == A[j,i] for all 0 i,j < N. Try to do this so that you examine each array element at most once.

Originally, line 6 was: return (i == N); my friend said that was wrong because it was only returning the rows....
Last edited on
JLBorges solution is equivalent since once you check (for example) i=1,j=2: A[1][2] == A[2][1], you need not check the condition where i=2,j=1 as that is simply A[2][1] == A[1][2]. Those are equivalent by the symmetry of equality.

And firedraco is right; A[i,j] is not what you want to use. For a two dimensional array, you need to use A[i][j] because when the comma operator is invoked, A[i,j] is the same as simply A[j].
wow, i dont know how i got the commas in there, X_X sorry about that, a typo...thank you for the pick up!
Thank you all for the assistance...
Last edited on
In JLBorges' solution, he used ++i and ++j rather than i++ and j++. doesnt this increase the value before using it? or for this case does it not matter which is used?
That's a preincrement, so it increases the value before using it; however, note that the expression ++i doesn't use the value of i anywhere so it is immaterial whether 'i' is incremented before or after the rest of the expression is evaluated. Preincrement is generally preferred simply because it obviates the need to create a temporary copy of i to use in the expression before incrementing it.
oooh icic! thank you again, Zhuge!
If anyone wouldn't mind, in the next review part (there are 14 questions, i've done 9....) My professor doesn't post the solutions so I am at a loss for someone to check with...i dont share any classes with other classmates and 90% of the class doesnt go to class anymore or just simply dropped.

Examine the following functions (Q12 & Q14):

 ``123456789`` ``````bool what(int A[], int size){ bool result = true; for(int i = 0; i < size; i++) for (int j = i + 1; j < size; j++) if(A[i] == A[j]) result = false; return result; }``````

What I suspected the above function did:
This function takes in 2 arguments and returns true if the next value in the array is different through each iteration. However, will return false if any of the values are the same.

 ``123456789`` ``````bool where(int A[], int size, int start, int k){ int i = start; while(i + k < size && A[i] <= A[i+k]) i += k; return (i + k >= size); }``````

What I suspected the above function did:
This function looks to see if the start element is less than or equal to some element k after the start element. This function returns false if adding k to the start element exceeds the bounds.

Would I be correct in both of my deductions? (last 2 questions I would really like to have checked)
Last edited on
You are correct about the first function. It returns whether or not all elements in the array are unique.

I'm not sure what the second one does. I can see what it's doing, but I don't understand why you would do it.

It goes through every k-th element after the starting element, and checks that it is greater than or equal to the the last element that was checked. (Basically, that the values at k intervals are steadily increasing.)

The function returns true if each k-th element is greater than or equal to the previous k-th element.
Last edited on
thank you so much, fg109! These were just function analysis my professor gave me, I tried to message him but he hasn't responded yet. Awesome, so happy right now.
Hi again... I know I'm kinda being troublesome asking a lot of questions, but I'm really new to this stuff so I'm just trying to learn, got a test tomorrow...

So this time, I'm writing a function:
`int column(int A[][COLS], int size, int col)` that returns the sum of
the numbers in column col in the 2D array A with size number of rows.

My attempt at the solution after about an hour:
 ``123456789`` ``````const int COLS = 7; const int size = 7; int column(int A[][COLS], int size, int col){ int sum = 0; for (col = 0; col < COLS; col++) sum += A[size][col]; return sum; }``````

when i execute this program, it is blank...I would really like to know what I did wrong. It made sense to me because it doesn't look at rows so it could be constant, so col had to be incremented. Did I not create a correct sum code for the columns?
You need to review arrays, and also you either misunderstood the problem or else you do not know how to do what you want correctly.

The reason that your program isn't giving you the output you expected is because your array index goes out of range.

For example, an array of size 5 has 5 elements. Their indices are 0, 1, 2, 3, and 4. An index of 5 is outside of the array.

Similarly in your problem, the array has 7 (size) rows. You are trying to access values at row 7, but remember that the rows are numbered from 0 to 6.

As for how you are misunderstanding the problem, imagine that these are the values stored in your 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49```

Numbers 1-7 correspond to the first row, 8-14 to the second, 15-21 to the third, etc.

If you were to call your function like so: `column(A, 7, 3)`, then if your code is correctly written, it would give you:

4 + 11 + 18 + 25 + 32 + 39 + 46 = 155

Therefore, it sums the values contained in A[0][3], A[1][3], A[2][3], ..., A[6][3].

EDIT: messed up column index
Last edited on
OOOH i see...I should have had column a constant value and summed the row value corresponding to the column. so i pretty much flipped it. thank you so much, fg109
Topic archived. No new replies allowed.