-
Assignment help
Ok, I have an assignment for my programming class but I really don't understand what the question is asking of me. I was wondering if someone could help me understand what I'm actually supposed to do. Here is the question:
"Secion 8.2 had a code fragment in which two nested for loops initialized the array ident for use as an identity matrix. Rewrite this code, using a single pointer to step through the array one element at a time. Hint: Since we won't be using row and col index variables, it won't be easy to tell where to store 1. Instead, we can use the fact that the first element of the array should be 1, the next N elements should be 0, the next element should be 1, and so forth. Use a variable that keeps track of how many consecutive 0s have been stored; when the count reaches N, it's time to store 1."
Here is the code it refers to from section 8.2:
Code:
#define N 10
float ident [N] [N];
int row, col;
for (row = 0; row < N; row++)
for (col = 0; col < N; col++)
if (row == col)
ident [row] [col] = 1.0;
else
ident [row] [col] = 0.0;
I don't know if any of you guys will need this but this could be some helpful information from the section 8.2:
"In mathematics, an identity matrix has 1's on the main diagonal, where the row and column index are the same, and 0's everywhere else."
Please help. I have no idea what this question is asking me to do maybe because I don't understand the idea of an "identity matrix."
-
But surely you can see what your example code does and how it sets ident [row] [col] = 1.0;
Draw an example out on paper and see what it looks like
Just do the same, with the suggested counters to do the same thing
float *flattenedArray = &ident[0][0];
Oh, and you might like to mention to your tutor that array flattening isn't within the standard.
http://www.eskimo.com/~scs/C-faq/q6.19.html
-
I was just wondering if this might be good start or somewhat close to what I need?
Code:
#define N 10;
float a[N] [N];
int *p;
for (p = &a[0] [0]; p <= a[N-1] [N-1]; p++)
[ Something here, I'm not really sure what though. Some conditions
that will make this thing an identity matrix. ];
-
Yeah, something like that - when you've fixed the types, and added the counters as per the hints in the assignment.
-
what do you mean by fix the types?
-
Well you have an int pointer and an array of floats....
-
Would this be an identity matrix then?
10000000
01000000
00100000
00010000
00001000
00000100
00000010
00000001
-
I'm not sure ? I don't really understand what they are.
-
From dictionary.com
identity matrix
n.
A square matrix with 1's along the diagonal from upper left to lower right and 0's in all other positions.
or
identity matrix
n : a scalar matrix in which all of the diagonal elements are unity [syn: unit matrix]
So I think that my example was right...hope it helps you somehow!
-
yes, I used the orginal code and printed it and that's exactly what it looked like. Now the question is, how do I get it do do it with pointers. Can someone please just give me a hint because the one in the book isn't working for me. I understand what the hint is saying but putting it in to code is giving me some trouble.
is this on the right track:
Code:
#include <stdio.h>
#include <stdio.h>
#define N 10
int main()
{ float a[N][N];
float *p;
int zeros;
a[0][0]=1;
for (p = &a[0][0]; p <= &a[N-1][N-1]; p++)
for (zeros = 0; zeros <= N; zeros++)
{ if (zeros == N)
*p = 1.00;
else
*p = 0.00;
}
return 0;
}
For some reason this code makes every element of the matrix equal to 1.
-
Yeah, you are only missing one thing (besides the typos and errors).. shouldnt you be incrementing p in the second for loop?
-
I don't understand why I would want to increment p again in the second for loop?
-
> For some reason this code makes every element of the matrix equal to 1.
Because your inner loop finishes when zeros == N
You're on the right track, but you only need a single loop
Code:
for (p = &a[0][0]; p <= &a[N-1][N-1]; p++) {
zeros++;
if ( zeros == N ) {
*p = 1.0;
zeros = 0;
} else {
*p = 0.0;
}
}
-
I understand all but the:
What is this line of code for? Thanks for the help by the way. I tried something like this with one for loop but I didn't know how to make zeros++ increment with p++. I was doing:
Code:
for (p = &a[0][1]; p <= &a[N-1][N-1]; p++, zeros++)
which was giving me weird output.
-
ok, nevermind. I understand the line now. if it gets to that point then it needs to reset back to 0 so it can post the 0s over again. sorry for the confusion. thank you for the help though.