Well... you have to make it syntactically correct C.
Printable View
Well... you have to make it syntactically correct C.
i tried to make it work
and it doesnt print anything
it just terminates the program
although i set the arra to be all zeros
??
Code://start calculating the long addition procces
for (kndex = 0; kndex < cols; kndex++)
{
for (index = 0; index < rows; index++)
{
arr[kndex]=arr[kndex]+matrix[index][kndex];
}
}
carry = 0;
//loop from the least significant "digit" to the most significant "digit"
for(index=cols-1;index>=0;index++){
number = arr[index] + carry;
arr[index] = number % 10;
carry = number / 10;
}
for (kndex = 0; kndex < cols; kndex++)
{
printf("%d ",arr[kndex]);
}
transgalactic2, indent your code properly. Post the smallest and simplest compilable code that demonstrates the problem.
I have been repeating this to you for a very, very long time. If you are not going to listen, I am not going to help.
this is the simplest code
the first part is taking the sum of all colums into one array
the second part is supposed to do the proccess i described in the start
i built the second part uppon your algorithm
and my main matrix was
1 1 1
2 2 2
so the array was
3 3 3
but when it enters your loop it should print 333
instead it going to endless loop
??
Code://start first part
for (kndex = 0; kndex < cols; kndex++)
{
for (index = 0; index < rows; index++)
{
arr[kndex]=arr[kndex]+matrix[index][kndex];
}
} //end first part
carry = 0; //start second part
for(index=cols-1;index>=0;index++)
{
number = arr[index] + carry;
arr[index] = number % 10;
carry = number / 10;
}
for (kndex = 0; kndex < cols; kndex++)
{
printf("%d ",arr[kndex]);
} //end second part
No, it is not the simplest code. If it is the simplest code then you are doing something wrong since the "first part" is completely wrong - there is no point computing the sum of all the columns into one array when you can create that one array from scratch. This is an example of what I am talking about:Quote:
Originally Posted by transgalactic2
Look at how I concentrate on the problem. Look at how you can compile and run the code as-is.Code:#include <stdio.h>
int main()
{
int digits[50] = {0};
int i;
int carry;
digits[47] = 8;
digits[48] = 19;
digits[49] = 10;
carry = 0;
for (i = 49; i >= 0; --i)
{
int number = digits[i] + carry;
digits[i] = number % 10;
carry = number / 10;
}
for (i = 0; i < 50; ++i)
{
printf("%d", digits[i]);
}
putchar('\n');
return 0;
}
i need to shift the whole array the the right in order that proccess to work
my array looks like
|1|2|3|0|0|0|....|0|0|0|
i need it to look like |0|0|...|1|2|3|
i tried to do it like this
??
Code:for (index = 0; index < 50; index++)
{
temp4=digits[49-(cols-1)+index];
digits[49-(cols-1)+index]=digits[index];
digits[index]=temp4;
}
That does not make sense. The process should work as-is.Quote:
Originally Posted by transgalactic2
If you do want to "shift" it, how will that work? A right shift would mean discarding the least significant "digit".
your code is ok
it works fine
but my whole program works on
this |1|2|3|0|0|0|....|0|0|0|
kind of array for which i used only the |1|2|3| part
in order to do this operation like you showed in your code
i need it to look like |0|0|...|1|2|3|
Code:/* your array is shifted them to the right i need to do that to
digits[47] = 8;
digits[48] = 19;
digits[49] = 10;
*/
for (index = 0; index < 50; index++)
{
temp4=digits[49-(cols-1)+index];
digits[49-(cols-1)+index]=digits[index];
digits[index]=temp4;
}
Ah, but you don't. In your case you are using a little-endian format, so instead of looping in reverse, loop in a forward direction. The algorithm is otherwise the same.Quote:
Originally Posted by transgalactic2
the problem is with the zeros
if my number is
3|3|3|0|000.........
i need to cut the zeros
so i could say if you see a zero then stop printing
but what if
my array is
3|0|3|0|000......... in that case it whould print only 3 instead of 303
thats why i need it to shift the the right like you did
the problem with that i am afraid that if i will get all the cells full
then by "shifting" it will change the number i am suppossed to print
??
Code:for (index = 0; index < 50; index++)
{
digits[49-(cols-1)+index]=digits[index];
}
when i copy the value to the other end i need to put other value to cell i just copied from
if i put zero it will work for such numbers as |3|3|3|....|0| =>|0|0|0|....|3||3||3|
but if i get a number like all 9
|9|9|9|...|9| then it would change the number instead of shifting..
what to do with the cell i just copied from
???
When printing, scan from the right until you find a non-zero, then keep printing until the end. Basically, you are just ignoring insignificant zeroes. If you scan to the end (i.e., the first element, all the way on the left) without finding a non-zero, the number itself is zero.Quote:
Originally Posted by transgalactic2
when i tried to do your example on my type of array
and i got a line of zeros
how to shift it to the left if we get a number that goes out of the
boundres
because our number was 1000
and we had only 3 places
??
Code:digits[0] = 8;
digits[1] = 19;
digits[2] = 10;
carry = 0;
for (index = 49; index >= 0; --index)
{
int number = digits[index] + carry;
digits[index] = number % 10;
carry = number / 10;
}
for (index = 0; index < 50; ++index)
{
printf("%d", digits[index]);
}
putchar('\n');
I notice that you are still looping in reverse.Quote:
Originally Posted by transgalactic2
As I said, do not attempt to "shift". Just ignore the leading zeroes, e.g.,Quote:
Originally Posted by transgalactic2
Code:#include <stdio.h>
#define NUM_DIGITS 50
int main()
{
/* Use little-endian format. */
int digits[NUM_DIGITS] = {10, 19, 8};
int i;
int carry;
/* Normalise such that each digit is a numeral in base 10. */
carry = 0;
for (i = 0; i < NUM_DIGITS; ++i)
{
int number = digits[i] + carry;
digits[i] = number % 10;
carry = number / 10;
}
/* Find the first non-zero. */
for (i = NUM_DIGITS - 1; i > 0 && digits[i] == 0; --i);
/* Print the significant digits. */
for (; i >= 0; --i)
{
printf("%d", digits[i]);
}
putchar('\n');
return 0;
}
i get 000000000000000000000000000000000000....00001000
when i run your code
i need only 1000
??