C: Problem with initializing array values to zero - c

I'm trying to construct the following 14*14 array i C: [I 0; 0 -I], that is a 7*7 identity matrix upper left, minus the identity lower right and zeros otherwise.
This is the method:
#define DIM 7
double S[2*DIM][2*DIM];
for(i = 0; i < DIM; i++){
for(j = 0; j < DIM; j++){
if(i == j){
S[i][j] = 1.0;
S[i+7][j+7] = -1.0;
}
else{
S[i][j] = 0.0;
}
}
}
This works fine for all the diagonal elements; however, some elements of the array get initialized to crazy values; for example, 13,6 gets initialized to
68111186113812079535019899599437200576833320031036694798491976301968333351950125611739840800974137748034248687763243996679617222196278187875968953700681881752083957666277350377710107236511681624408064.000000
This seems to be happening consistently (at least thrice) to entries 11,13, 12,9, 12,10, 13,12 and 13,6.
Can anybody tell me what's at play here or provide an alternative solution?
Cheers!
EDIT: The weird entries aren't consistent.
EDIT2: Typo: 13,12, not 13,15

Your loop only covers the upper left quadrant, so the off-diagonal elements in the other quadrants are not initialized and contain garbage. Your loop should go up to 2*DIM for each dimension, so that the off-diagonal elements are zeroed, and then your conditional for the diagonal elements should just be a little more complex to decide which value to set the diagonal element to.
Note that [13, 15] is entirely outside of this array!

You can initialize the whole array with zeros, then set only the diagonal
double S[2*DIM][2*DIM] = {0};
for (i = 0; i < DIM; i++) {
s[i][i] = 1;
s[i + DIM][i + DIM] = -1;
}

You are never writing to s[i][j] for i != j and i >= DIM or j >= DIM. If your array has automatic storage (is "local") it contains arbitrary init values.

I would say most of the elements that are outside 7x7 will not be initialized at all unless i == j (diagonal elements).
What do you want to initialize them to?

That's because your not initializing those elements. Here is some better code:
#define DIM 14
double S[DIM][DIM];
for (i = 0; i < DIM; i++) {
for (j = 0; j < DIM; j++) {
if (i == j) {
if (i < 7) {
S[i][j] = 1.0;
} else {
S[i][j] = -1.0;
}
} else {
S[i][j] = 0.0;
}
}
}

You never initialize values with an i or j between DIM + 1 and 2*DIM. So when you look at a value stored in one of those positions, you see whatever was there before that space was accessed by your matrix.

Related

Check if an array of integers contain a integer a[i] such that a[i] = a[j]+a[k] where j,k<i and j!=k

This is what I come up with
public static boolean pairEqualSum(int[] a) {
for (int i = 2; i < a.length; i++) {
for (int j = i - 1; j >= 1; j--) {
for (int k = j - 1; k >= 0; k--) {
if (a[i] == a[j] + a[k]) {
System.out.println(i + " " + j + " " + k);
return true;
}
}
}
}
return false;
}
I can only solve this with three loops which makes its time complexity O(n^3).
Is there a better way to solve this?
First we can solve another task: given an array of items, answer whether it is possible to create a number X from the sum of two elements.
In order to do this we can sort the array and introduce left and right locations.
L R
1 3 8 13 15 19 44 178
Now we can perform two actions:
Increase the sum by moving L to the right
Decrease the sum by moving R to the left
Using these two actions we can answer the question in O(n), by always maintaining L+Ras close to X as possible. This approach results in overall algorithm complexity of O(n*lg(n)) because of sort.
Using this algorithm you can achieve the final complexity of O(n*n) (considering that you'll sort array using all the items only once, and then just "enable" elements of array while moving X).
How about this way?
int[] arr[];
hashtable = init_hashtable();
for(int i = 2; i < arr.length; i++)
{
if (hastable.Contains(arr[i])) return true;
//we need combinations from orange vertical column
for(int j = 0; j < arr.length - 1; j++)
{
int combination = arr[j] + arr[i-1];
if (arr[i] == combination || !hashtable.Set(combination))
return true;
}
}
return false;
yellow is main iteration in array,
green j,k combinations,
black are mirror combination,
red are j = k,
in each iteration we need to insert new combinations from orange vertical column

Understanding two dimensional arrays

Here's the code and wondering if you can help me understand it.
/* Two dimensional array */
#include <stdio.h>
void main() {
int i, j, sum[2], mean[2];
int mark[3][2] = { { 34, 56}, { 48, 65}, { 53, 59} };
for (j = 0; j < 2; j++)
sum[j] = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
sum[j] = sum[j] + mark[i][j];
}
}
for (j = 0; j < 2; j++)
mean[j] = sum[j] / 3;
printf("Average mark in Mathematics is %d\n", mean[0]);
printf("Average mark in Chemistry is %d\n", mean[1]);
}
My understanding of it so far....
Define data types i, j, sum[2], mean[2] as integers.
Initialising the array....mark is data type int, the array should have 3 rows and 2 columns.
First for loop, j initialised at 0, condition: j has to be less than 2, update: add one onto the value of j. Sum of j = 0.
Also for 2nd loop, i initialised at 0, condition: i has to be less than 3, update: add one onto the value of i.
Similar for the next line that uses the for loop and value j.
I'm a bit confused about the syntax:
sum[j] = sum[j] + mark[i] [j]; does this mean, work out the sum of j and add it to the marks contained in the array displayed as [i] and [j].
After this is completed then similar j loop though not sure how this interacts with the previous loops.
Mean calculated and values printed to the screen.
When I've looked at the worked example...
sum[0] = 0 and sum[1] = 0, I don't really understand why sum[1] is also 0.
Firstly, i=0 and j=0,
sum[0] = sum[0] + mark [0,0]
then j=1
sum[1]=sum[1]+mark[0,1]
then
i=1, j=0
sum[0] = sum[0] + mark [1,0]
then
sum[1] = sum[1]+mark[1,1]
then i = 2, j=0
sum [0] = sum[0]+ mark[2,0]
then
sum[1] = sum[1]+ mark[2,1]
What is confusing me a bit is how the loops are interacting with each other and the values of i and j throughout.
I know that the 2d array would be in a table (that I can't seem to format here).
Would appreciate if anyone could shed some light on this.
sum[j] = sum[j] + mark[i][j]; can be simplified as sum[j] += mark[i][j];. It adds the contents of the cell at row i, column j of the 2D matrix mark to the jth element of array sum.
Accessing an element of a 2D array is written mark[i][j] in C, not mark[i, j].
Note that mark[i, j] is not a syntax error: the expression i, j is a comma expression, evaluating i, then discarding it and evaluating j. It is therefore the same as mark[j] which is not a matrix cell but a reference to the jth row of the 2D matrix.
Nope, an array is in fact a pointer (aka an address). When you define int sum[2], sum is the address of the first element of your array of two integer.
So sum is an int* or int[] (same).
Mark is an 2d array. Mark is in fact an array of array. So Mark contain addresses, and thoses addresses are the beggining of some arrays.
(In fact it can be different in the memory, but the compiler do the work).
Mark is a int**, or an address of address of int.
When you do:
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
sum[j] = sum[j] + mark[i][j];
}
}
It's like if you were saying
for each line i in the array, I want to do:
for each value j in the line, I want to do:
...;
Like this, you work on every "line" (or column, visualize the way you want), and for every "line", you work on every value.

A way to go over the diagonals of an array of size [6][7]

I'm building in C language, a game called 4-in-a-row or Connect Four, for a fast review of the game you can see here:
http://en.wikipedia.org/wiki/Connect_Four
so, I have a 2 dimensional array of size [6][7], and I want to check in diagonal if there are 4 tokens which are "*" or "o" that are defined as a chars which are in a a row. I'm trying to write a function that after each play, it sums up all the possible diagonals and see if the sum is 4 for example, or if we want to check in pairs, if we get three similar pairs then there are 4 equal tokens in a row, so in this case the sum is 3, and so on..
for all I know, there are 12 different different diagonals (every 6 on different direction), how do u suggest me to write this function while being the most effective? and also including all the possibilities with less that 16 lines of code.
any kind of help would be appreciated!
here is an example of what I did:
int CheckDiagonal_1(char matrix[Rows][Columns])
{
int s_count = 0;
int o_count = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 5; j >= 3; j--)
{
for(int k = 0; k <= 3; k++)
{
if(matrix[j-k][i+k]== matrix[j-k-1][i+k+1]) count ++;
if(count==4) return count;
}
count = 0;
}
}
return 0;
}
Diagonals are sequences where
i == j + c for i from (0,height) and c (-width, height)
or i == -j + c.
So if goal to write code that fits into small number of lines - just write loops that go over i {0-6} and check for indexes to fit in range. Something like
for (int c= -7; c < 7; c++)
{
int starsOnDiag = 0;
for(int i = 0; i < 7; i++)
{
starsOnDiag += !indexesInRange(i, j) ? 0 :
cell[i, i+c] == '*' ? 1 : 0;
}
... // other diagonal and check for other symbol
}

A for loop in C using an array

I've created an array named a that can hold 100 double values,
double a[100];
I set the first element of the array a to NUM, which is a symbolic constant defined early in my code.
a[0] = NUM
I'm curious as to how I would write a for loop that sets each remaining value of a to the value of the preceding element plus 0.1. For example, the second element in the array is the first plus 0.1. I've tried doing
for(i=1; i<=99; i=+0.1)
But I think something is wrong with my initialization of i
Use i to index the array, not to store the value you should put on the array. Remember you can use expressions to access the array, like a[i - 1]
for (i = 1; i < 100; i++)
a[i] = a[i - 1] + 0.1;
int i;
for(i = 0; i < 100; i++)
a[i] = NUM + 0.1 * i;
dont forget to tell the type int !
int i = 0;
for(i = 0; i < 100; i++){
if (i == 0)
a[i] = NUM;
else
a[i] = a[i - 1] + .1;
}
Your array definition includes the step. So your array would run about 1000 times, at 1, 1.1, 1.2, but a[1.1] isn't a valid index of your array. Use i to index the array, and then retrieve the previous value to set the next.
From your question I can understand that this is one of your first program in C/C++, so I think that you need to start from basic things and learn how to do it properly, before doing it elegantly.
http://ideone.com/RGZgXL
for(i = 0; i < ARRAY_SIZE; i++) {
if(i == 0) { // if we are on the first element, set it to NUM
array[i] = NUM;
} else { // otherwise make the sum
array[i] = array[i-1] + STEP;
}
}
In the link you'll find the code and some comments that I hope will help you in understanding it.
Cheers

Iterate ALL the elements of a circular 2D array exactly once given a random starting element

We are given a 2-dimensional array A[n,m] with n rows and m columns and an element of that array chosen at random R.
Think of the array as being circular in that when we visit A[n-1, m-1] the next element we visit would be A[0, 0].
Starting with element R, we want to visit each element exactly once and call function foo() before moving to the next element.
The following is my first implementation but there is a bug. The bug being that if we start at row x somewhere between 0 and n-1, we will not visit element from 0 to x-1 in that column.
// Init - pretend rand() always returns valid index in range
curr_row = rand();
curr_col = rand();
// Look at each column once
for (int i = 0; i < m; ++i)
{
for (; curr_row < n; ++curr_row)
{
foo(A[curr_row][curr_col]);
}
curr_row = 0;
curr_col = (curr_col + 1) % m;
}
What is a clean way to do this traversal such that we meet the above requirements?
Just move to the next index, and check whether you are back at the start, in which case, stop:
// should be something that guarantees in-range indices
curr_row = rand();
curr_col = rand();
int i = curr_row, j = curr_col;
do {
foo(A[i][j]);
++j;
if (j == n) {
j = 0;
++i;
if (i == m) {
i = 0;
}
}
}while(i != curr_row || j != curr_col);
This doesn't do what your implementation does, but what the question title asks for.
quite rusty with c , but it should be the same:
// Init - pretend rand() always returns valid index in range
curr_row = rand();
curr_col = rand();
//first row
for(int j=curr_col;j<m;++j)
foo(A[curr_row][j]);
//rest of the rows
for(int i=(curr_row+1)%n;i!=curr_row;i=(i+1)%n)
for(int j=0;j<m;++j)
foo(A[i][j]);
//first row , going over missed cells
for(int j=0;j<curr_col;++j)
foo(A[curr_row][j]);
if you care a lot about performance , you can also divide the second loop so that there won't be a "%" at all .
another alternative , since C has 2d arrays in a simple array:
// Init - pretend rand() always returns valid index in range
curr_row = rand();
curr_col = rand();
int start=curr_row*m+curr_col;
int maxCell=n*m;
int end=(start-1)%maxCell;
for(int i=start;i!=end;i=(i+1)%maxCell)
foo(A[i]);
foo(A[end]);
could have a tiny math bug here and there ,but the idea is ok.
A[curr_row, curr_col] is not the syntax used to access a member of a multidimensional array; instead, you want A[curr_row][curr_col], assuming the array was declared correctly. A[curr_row, curr_col] will invoke the comma operator, which effectively computes the first value, then throws it away and calculates the second value, then indexes the array with that value.

Resources