Visualising recursion in C - c

I am trying to understand how recursion works in the factorial function. What should I print, so that I can see what actually happens during each recursion call?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
long factorial(long number) {
if(number <= 1) {
return 1;
} else {
return number * factorial(number - 1);
}
}
int main() {
int i;
for(i = 0; i <= 10; i++) {
printf("%2d ! = %ld\n", i, factorial(i));
}
return 0;
}

The following code
#include <stdio.h>
#include <stdlib.h>
long factorial (int level, long number){
int result;
printf("[%03d] Calculating: %d\n", level, number);
if(number <= 1)
{
result = 1;
}
else
{
result = number * factorial(level+1, number - 1);
}
printf("[%03d] Returning: %d\n", level, result);
return result;
}
int main(){
int i;
for(i = 0; i <= 10; i++)
{
printf("%2d ! = %ld\n", i, factorial(0, i));
}
return 0;
}
gives this output
[000] Calculating: 10
[001] Calculating: 9
[002] Calculating: 8
[003] Calculating: 7
[004] Calculating: 6
[005] Calculating: 5
[006] Calculating: 4
[007] Calculating: 3
[008] Calculating: 2
[009] Calculating: 1
[009] Returning: 1
[008] Returning: 2
[007] Returning: 6
[006] Returning: 24
[005] Returning: 120
[004] Returning: 720
[003] Returning: 5040
[002] Returning: 40320
[001] Returning: 362880
[000] Returning: 3628800
10 ! = 3628800
Where [XXX] is the current level of recursion.

Try this
long factorial (long number){
if(number <= 1){
return 1;
}else{
return number * factorial(number - 1);
}
}
Assume we pass 4 as first parameter, then function evaluates to:
long factorial (long number /*==4*/){
if(4 <= 1){//false
return 1;
}else{
return 4 * factorial(4 - 1);
}
}
Note return in else can't finish, because it calls another function - factorial (4-1).
So 4 must be multiplied by the result of factorial (3). So it goes and executes factorial(3).
Now we get
long factorial (long number /*==3*/){
if(3 <= 1){//false
return 1;
}else{
return 3 * factorial(3 - 1);
}
}
But again the return here can't finish, since it calls factorial for 3-1=2.
Now one needs to evaluate factorial (2), result of which will be multiplied by 3 and passed to previous return. Again:
long factorial (long number /*==2*/){
if(2 <= 1){//false
return 1;
}else{
return 2 * factorial(2 - 1);
}
}
We have similar problem. But now when executing factorial (2-1) the function will immediately return 1, hence above line will return 2.
This result will be plugged to the result of previous return, giving 2*3; this result - 6 - will be plugged in previous return and yield 6*4, which is the final result 24.

For something ridiculous, factor out the body of factorial:
long factorial_body(long number) {
if(number <= 1) {
return 1;
} else {
return number * factorial(number - 1);
}
}
Then, rewrite factorial to call factorial_body, printing some stuff around the call.
long factorial (long number) {
static char output[4096];
static int level;
static int index;
int saved_index;
long result;
switch (level++) {
case 1: index += snprintf(output + index, sizeof(output) - index, "= ");
default: index += snprintf(output + index, sizeof(output) - index,
"%ld x ", number + 1);
case 0: snprintf(output + index, sizeof(output) - index,
"factorial(%ld)\n", number);
}
saved_index = index;
printf("%s", output);
result = factorial_body(number);
index = saved_index;
snprintf(output + index, sizeof(output) - index,
--level ? "%ld\n" : "= %ld\n", result);
printf("%s", output);
return result;
}
factorial(10) will generate the following output:
factorial(10)
= 10 x factorial(9)
= 10 x 9 x factorial(8)
= 10 x 9 x 8 x factorial(7)
= 10 x 9 x 8 x 7 x factorial(6)
= 10 x 9 x 8 x 7 x 6 x factorial(5)
= 10 x 9 x 8 x 7 x 6 x 5 x factorial(4)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x factorial(3)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x factorial(2)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x factorial(1)
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2
= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 6
= 10 x 9 x 8 x 7 x 6 x 5 x 24
= 10 x 9 x 8 x 7 x 6 x 120
= 10 x 9 x 8 x 7 x 720
= 10 x 9 x 8 x 5040
= 10 x 9 x 40320
= 10 x 362880
= 3628800
I am dubious if the above output will help enhance your understanding of recursion. But, working to understand how the printing code works probably would lead to a better understanding of recursion.

Instead of print statements add whatever you would print to an array or something and then print them out in the order that they were inserted (fifo) after the recursive function is finished.

This should give you a trace of the execution of factorial, with indentation:
long factorial(long number, int depth){
printf("%*s enter, number = %d\n", 4*depth, "", number);
long result = 1;
if (number > 1) result = number * factorial(number-1, depth+1);
printf("%*s exit, result = %d\n", 4*depth, "", result);
return result;
}

Related

Fill matrix with two numbers in a spiral

In the middle of matrix is number x. Matrix is filled with x in a spiral, while the rest is filled with y.
Example:
int x=4, y=6, v=7, s=9;
OUTPUT:
6 6 6 6 6 6 6 6 4
4 4 4 4 4 4 4 6 4
4 6 6 6 6 6 4 6 4
4 6 4 4 x 6 4 6 4
4 6 4 6 6 6 4 6 4
4 6 4 4 4 4 4 6 4
4 6 6 6 6 6 6 6 4
Note: In the output instead of the letter x program should print 4, but I wrote x to make it easier for understanding task.
Spiral filling looks like --> this
#include <stdio.h>
int main() {
int v = 7, s = 9, x = 4, y = 6, mat[7][9], i, j, l, k;
for (i = 0; i < v; i++)
for (j = 0; j < s; j++)
mat[i][j] = y;
for (i = 0; i < v; i++) {
for (j = 0; j < s; j++) {
// spiral goes here
// mat[i][j] = x;
}
}
for (i = 0; i < v; i++) {
for (j = 0; j < s; j++)
printf("%4d", mat[i][j]);
printf("\n");
}
return 0;
}
EDIT: After applying suggestions, now the whole matrix is filled with y. How to make it fill spiral with x?
Below is a program for constructing a spiral from the center in an s by v matrix.
Here's the algorithm:
The matrix is ​​filled with y values
Calculating the center, the cursor cx, cy is set there, 0 is written
The spiral drawing direction is set, dir, 1 - left, 2 - down, 3 - right, 4 - up, first 1
Draw 1 step by setting x value to matrix
If the value in the matrix for dir+1 (if dir+1=5 then dir=1) = y (empty) then dir is incremented by +1. If not (center (0) or already drawn (x)) then dir remains the same.
If beyond the edge of the matrix, then dir is incremented +1 as if empty, but nothing is drawn.
Step 4 is repeated s*v/2 times
The program is compiled with the gcc compiler.
Output:
cx - 4, xy - 3
6 6 6 6 6 6 6 6 4
4 4 4 4 4 4 4 6 4
4 6 6 6 6 6 4 6 4
4 6 4 4 0 6 4 6 4
4 6 4 6 6 6 4 6 4
4 6 4 4 4 4 4 6 4
4 6 6 6 6 6 6 6 4
Required:
6 6 6 6 6 6 6 6 4
4 4 4 4 4 4 4 6 4
4 6 6 6 6 6 4 6 4
4 6 4 4 x 6 4 6 4
4 6 4 6 6 6 4 6 4
4 6 4 4 4 4 4 6 4
4 6 6 6 6 6 6 6 4
#include <stdio.h>
/*
Increase dir
*/
int incdir (int dir) {
// direction, 1 - left, 2 - down, 3 - right, 4 - up
dir++;
if (dir==5) dir=1;
return dir;
}
/*
Is coord in matrix
*/
int inmat (int y, int x, int v, int s) {
// direction, 1 - left, 2 - down, 3 - right, 4 - up
if (x<0 || x>=s) return 0;
if (y<0 || y>=v) return 0;
return 1;
}
/*
Value in some direction
*/
int dirval(int cx, int cy, int v, int s, int dir, int mat[v][s]) {
// direction, 1 - left, 2 - down, 3 - right, 4 - up
if (dir==1) {
if (!inmat(cy, cx-2, v, s)) return -1;
return mat[cy][cx-2];
}
if (dir==2) {
if (!inmat(cy+2, cx, v, s)) return -1;
return mat[cy+2][cx];
}
if (dir==3) {
if (!inmat(cy, cx+2, v, s)) return -1;
return mat[cy][cx+2];
}
if (dir==4) {
if (!inmat(cy-2, cx, v, s)) return -1;
return mat[cy-2][cx];
}
}
int main() {
int v = 7, s = 9, x = 4, y = 6, mat[7][9], i, j, l, k;
// cursor
int cx, cy;
cx=((int)s/2);
cy=((int)v/2);
printf("cx - %4d, xy - %4d\n", cx, cy);
int dir=1; // direction, 1 - left, 2 - down, 3 - right, 4 - up
int dv;
for (i = 0; i < v; i++)
for (j = 0; j < s; j++)
mat[i][j] = y;
mat[cy][cx]=0; // start
// spiral goes here
for (i = 0; i < s*v/2; i++) {
if (dir==1) {
if (inmat(cy, cx-1, v, s)) mat[cy][cx-1]=x;
if (inmat(cy, cx-2, v, s)) mat[cy][cx-2]=x;
cx=cx-2;
}
if (dir==2) {
if (inmat(cy+1, cx, v, s)) mat[cy+1][cx]=x;
if (inmat(cy+2, cx, v, s)) mat[cy+2][cx]=x;
cy=cy+2;
}
if (dir==3) {
if (inmat(cy, cx+1, v, s)) mat[cy][cx+1]=x;
if (inmat(cy, cx+2, v, s)) mat[cy][cx+2]=x;
cx=cx+2;
}
if (dir==4) {
if (inmat(cy-1, cx, v, s)) mat[cy-1][cx]=x;
if (inmat(cy-2, cx, v, s)) mat[cy-2][cx]=x;
cy=cy-2;
}
dv=dirval(cx, cy, v, s, incdir(dir), mat);
if (dv==y || dv==-1) dir=incdir(dir);
}
for (i = 0; i < v; i++) {
for (j = 0; j < s; j++)
printf("%4d", mat[i][j]);
printf("\n");
}
return 0;
}
In the first iteration of the inner loop if (i == v / 2 && j == s / 2) is always false so you keep doing:
mat[l][k--] = x; // Decrement k
mat[l++][j] = y; // Increment l
Now add the inner-loop:
------ Increment k
|
for (k = j; k < s; k++) {
//if (i == v / 2 && j == s / 2) // Always false when i is zero
// mat[i][j] = x;
//else
{
mat[l][k--] = x; // Decrement k
mat[l++][j] = y; // Increment l
}
}
So you decrement k and the increment k in the for loop. In other words - k doesn't change and you have an endless loop. In that endless loop you keep incrementing l and use it as array index. That will access the array out of bounds and crash your program.

Recursive algorithm to fill increasing array { 1, 2, 2, 3, 3, 3, ... }

I have the following task:
Give a recursive algorithm for filling an array like this: one 1, two 2, tree 3, four 4, ... ,n n.
For example, with n = 4 the array is supposed to look like:
{ 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }
My attempt is
Function filling(T, k, n)
if (n = 0) do return 1
else if (0 <= k) do filling (T, n - k + 1, n);
else filling(T, k, n - 1);
fi
fi
with filling(T, k, n)
k: block start case of number n,
n: number,
T: array
As you correctly show, the function needs an array T, a starting index k of the area to fill, and a size of the task n, i.e. a maximum number for filling.
The algorithm can be described like this:
if n is zero, return;
otherwise:
perform filling from 1 till n-1,
then append n items of value n
The question arises, how should the last line know where it should start adding.
One simple answer is: the routine may return the index one past the filled area:
size_t filling(int T[], size_t k, size_t n)
{
if (n != 0)
{
k = filling(T, k, n - 1);
for (size_t i = 0; i < n; i++)
T[k + i] = n;
}
return k + n;
}
Make sure your array is at least n*(n+1)/2 items long, so that you don't overrun it.
You need to take care of two things here:
Number of digits to be insert in the array starting from 1, and
Count of a digit (which should be equal to digit itself) when inserting it in the array.
Since, the algorithm should be implemented using recursion, we should know the terminating condition of recursion.
Algorithm:
Insert digit in array.
Check if the count of digit inserted in array is equal to digit or not:
If count == digit then reset the count to 0 and increase digit by 1.
If count < digit go to step 1.
If digit to be inserted in array is greater than n, terminate the recursion.
Implementation in c++:
#include <iostream>
#include <vector>
void filling (std::vector <int>& arr, int k, int n) {
static int c;
if (c >= n) {
c = 0;
return;
}
if (k <= c) {
k = k + 1;
arr.push_back(c + 1);
} else {
k = 0;
c = c + 1;
}
filling (arr, k, n);
}
int main() {
std::vector <int> arr;
int n;
std::cout << "Enter a number:" << std::endl;
std::cin >> n;
filling (arr, 0, n);
for (const auto& x : arr) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
Output:
# ./a.out
Enter a number:
5
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
# ./a.out
Enter a number:
10
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10
# ./a.out
Enter a number:
1
1
# ./a.out
Enter a number:
4
1 2 2 3 3 3 4 4 4 4

How to get every digit from a number in C?

I have these variables:
int dividend;
 int divider;
And I have the function :
Divisibility7 (int num);
Those two variables will be at the main function, and the user will be asked to enter the dividend and divider, but in case the user enter the divider 7, then, the function above will be called.
The problem is that I have to follow specific criteria to do this. So let's say the user will enter with the dividend 7203. This happen :
I. Get the last digit of the number.
Last digit: 3
Ii. Multiply the last digit by 2
3 x 2 = 6
Iii. Get the value of the initial number, without the last digit.
720
Iv. Subtract the initial value without the last digit from the multiplication result.
fabs (720 - 6) = 714
V. Repeat the process until the result is a value less than or equal to 70
Vi. Compare the result with the table of contents (0, 7, 14, 21, 28, 35, 42, 49, 54, 63, 70) for
Determine whether or not the number is divisible by 7
Code :
int res;
int x;
int y;
int Divisibility7(int num) {
int res;
int x;
int y;
int z;
while(num > 70) {
x = num % 10; // get the last digit from the number
y = x * 2; // multiply the last digit by 2;
z = num/10; // get the first digits from the number
fabs(z - y); // subtract the first digits with the last digits;
}
}
In the part of the while, the final fabs(z-y) returns what I want, to be the first digits subtracting the last number, but the problem is that the while stop there, I have to do something to make this while go till 70 or less.
PS : I need to check if the final number from the iterations, it's a number multiplied by 7, how can I do that ? And I can't use mod for this.
You have not changed num in your while loop . Also you do no return the value . Hope the following code will be ok for you .
int Divisibility7(int num) {
int res,x,y,z;
while(num > 70) {
x = num % 10; // get the last digit from the number
y = x * 2; // multiply the last digit by 2;
z = num/10; // get the first digits from the number
num = abs(z - y); // subtract the first digits with the last digits;
}
if(num == 0 || num == 7 || num == 14 || num == 21 || num == 28 || num == 35 || num == 42 || num == 49 || num == 54 || num == 63 || num == 70) {
return 1;
}
else {
return 0;
}
}
Not sure, but I think this is what are you trying to do:
int main (void)
{
int number, lastDigitMultiplied;
scanf("%d", &number);
while(number > 70){
//get the last digit and multiply it by 2
lastDigitMultiplied = (number % 10) * 2;
//subtract the initial value without the last digit from the multiplication result.
number = number / 10 - lastDigitMultiplied;
}
if(abs(number) % 7 == 0)
printf("The result is %d and it is a multiple of 7", number);
else
printf("The result is %d and it is not a multiple of 7", number);
return 0;
}
for divisibility against 7, if you have a positive large bigint already formatted as a string, don't waste time with the regular method doing it 1 digit at a time.
powers of 10, mod 7, repeats every 6 rounds :
10^1 % 7 = 3 10^7 % 7 = 3 10^13 % 7 = 3
10^2 % 7 = 2 10^8 % 7 = 2 10^14 % 7 = 2
10^3 % 7 = 6 10^9 % 7 = 6 10^15 % 7 = 6
10^4 % 7 = 4 10^10 % 7 = 4 10^16 % 7 = 4
10^5 % 7 = 5 10^11 % 7 = 5 10^17 % 7 = 5
10^6 % 7 = 1 10^12 % 7 = 1 10^18 % 7 = 1
meaning, the effects of the digits mod 7 stay identical as long as they're in chunks of 6 digits at a time.
the largest multiple of 6-digits supported by double precision FP to full integer precision would be 12-digits at a time.
So the way to do it is to right-align the digits by padding leading edge zeros to ensure string length is a multiple of 12. Then simply add the digits onto each other, performing a single mod % 7 operation once every 9000 or so rounds of the addition loop
—- (that's when you run up against 2^53 limit; 9007 rounds if u wanna be pedantic about it)
example :
x = 71400535477047763120175175402859619447790
02233464423375355339031113233806609150957
x % 7 = 4
now try summing up chunks of 12 :
007140053547704776312017517540285961944779
002233464423375355339031113233806609150957
007140053547 7140053547
704776312017 711916365564
517540285961 1229456651525
944779002233 2174235653758
464423375355 2638659029113
339031113233 2977690142346
806609150957 3784299293303
----------------------------
3784299293303 % 7 = 4
it works exactly the same for any multiples of 6 : e.g. 6, 18, 24, and 30 --
00714005354770477631201751754
02859619447790022334644233753
55339031113233806609150957
312017 312017
517540 829557
285961 1115518
944779 2060297
002233 2062530
464423 2526953
375355 2902308
339031 3241339
113233 3354572
806609 4161181
150957 4312138
007140 4319278
053547 4372825
704776 5077601
----- -------
_ 5077601 % 7 = 4
00000000714005354770477631201
75175402859619447790022334644
23375355339031113233806609150957
464423375355339031 464423375355339031
113233806609150957 577657181964489988
000000007140053547 577657189104543535
704776312017517540 1282433501122061075
285961944779002233 1568395445901063308
---------------------------------------
1568395445901063308 % 7 = 4
00000000000000714005354770477
63120175175402859619447790022
33464423375355339031113233806
609150957
339031113233806609150957 339031113233806609150957
000000000000007140053547 339031113233813749204504
704776312017517540285961 1043807425251331289490465
944779002233464423375355 1988586427484795712865820
---------------------------------------------------
1988586427484795712865820 % 7 = 4
000000007140053547704776312017517
540285961944779002233464423375355
339031113233806609150957
000000007140053547704776312017 7140053547704776312017
517540285961944779002233464423 517540293101998326707009776440
375355339031113233806609150957 892895632133111560513618927397
892895632133111560513618927397 % 7 = 4

Getting Compiling Error

Here is the task I was given:
Write a function fact_calc that takes a string output argument and an integer input argument n and returns a string showing the calculation of n!. For example, if the value supplied for n were 6, the string returned would be
“6! x 6 x 5 x 4 x 3 x 2 x 1 x = 720”. Write a program that repeatedly prompts the user for an integer between 0 and 9, calls fact_calc and outputs the resulting string in a box of asterisks of the right size to surround the result. If the user inputs an invalid value, the program should display an error message and reprompt for valid input. Input of the sentinel -1 should cause the input loop to exit.
Here is my code:
#include <stdio.h>
/*Prototypes*/
void fact_calc(char [], int);
int main(void)
{
char calc[99];
int num1;
do{
printf("Enter an integer between 0 and 9 or -1 to quit: ");
scanf("%d", &num1);
fact_calc(calc, num1);
}while( num1 != -1 );
return (0);
} //end main
/*Place function definitions below*/
void fact_calc(char calc[], int num1)
{
if(num1 == 0)
char calc[] = "0! = 0 = 0";
else if(num1 == 1)
char calc[] = "1! = 1 = 0"
else if(num1 == 2)
char calc[99] = "2! = 2 x 1 = 2"
else if(num1 == 3)
char calc[99] = "3! = 3 x 2 x 1 = 6"
else if(num1 == 4)
char calc[99] = "4! = 4 x 3 x 2 x 1 = 24"
else if(num1 == 5)
char calc[99] = "5! = 5 x 4 x 3 x 2 x 1 = 120"
else if(num1 == 6)
char calc[99] = "6! = 6 x 5 x 4 x 3 x 2 x 1 = 720"
else if(num1 == 7)
char calc[99] = "7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040"
else if(num1 == 8)
char calc[99] = "8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320"
else if(num1 == 9)
char calc[99] = "9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880"
else
printf("Invalid Entry");
}
I am getting the error:
expected
expression
ch...
^
I still have to print out the string but I can't figure out what the error means or what I am doing wrong. Thanks in advance for the help!
char calc[] = "1! = 1 = 0"
Don't you think you should be ending this line with a ;
char calc[] = "1! = 1 = 0";
Similar fix needs to be done for the rest of the lines
Your program is doing something strange. I doubt it does what you expect.
You are declaring variables inside the body of if statements. These variables will only be in scope for the body, and you don't do anything with them.
Your program can be made to work. I suggest simply making fact_calc() return literal strings rather than try to assign to variables.
/*Place function definitions below*/
char const *fact_calc(char calc[], int num1)
{
if(num1 == 0)
return "0! = 0 = 0";
else if(num1 == 1)
return "1! = 1 = 0";
else if(num1 == 2)
return "2! = 2 x 1 = 2";
else if(num1 == 3)
return "3! = 3 x 2 x 1 = 6";
else if(num1 == 4)
return "4! = 4 x 3 x 2 x 1 = 24";
else if(num1 == 5)
return "5! = 5 x 4 x 3 x 2 x 1 = 120";
else if(num1 == 6)
return "6! = 6 x 5 x 4 x 3 x 2 x 1 = 720";
else if(num1 == 7)
return "7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040";
else if(num1 == 8)
return "8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320";
else if(num1 == 9)
return "9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880";
/* Invalid Entry */
return NULL;
}
Then your main function should declare a variable of type char const * to store the pointer returned by this function, and should check that the pointer is not set to NULL (and print "Invalid Entry" if the pointer was set to NULL).
You cannot assign strings (char arrays) in C. You should use strcpy() instead.
E.g.
strcpy(calc, "2! = 2 x 1 = 2");
Note that you should use strcpy() only if you are 100% certain calc is large enough to store the entire string. If not, use strncpy() instead. Example:
strncpy(calc, "2! = 2 x 1 = 2", 99);
calc[98] = '\0';
// a better way would be to pass 99 to your function, or to declare a macro
Finally, note that both 0! and 1! should be 1, not 0.

How to initialize each row of a 8x10 matrix with the right shifted value of the previous row

So what I'm trying to do is have the first row of the 8 x 10 matrix say 12345678910. The second line would say 10123456789. The third would say 91012345678. Ect. This is what I have so far. It just continues to count up.
#include<stdio.h>
#define ROWS 8
#define COLS 10
int main(int argc, char *argv[])
{
int A[ROWS][COLS];
int B[COLS][ROWS];
int x,y;
for (x=0; x<ROWS; x++)
{
for (y=0; y<COLS; y++)
{
A[x][y] = 1*x + y;
}
}
printf("=== Original matrix === \n");
for (x=0; x<ROWS; x++)
{
for (y=0; y<COLS; y++)
{
printf("%3d ", A[x][y]);
}
printf("\n");
}
First, just fill the first row with whatever you want in it. For example, for numbers counting up from zero:
for (y = 0; y < COLS; y++)
A[0][y] = y;
Once you have the first row filled, this will fill the remaining rows as you described (each row right-rotated by one from the previous row):
for (x = 1; x < ROWS; x++)
for (y = 0; y < COLS; y++)
A[x][(x + y) % COLS] = A[0][y];
The above starts filling from the second row (index of 1) onward, and always copies values from the first row. However, the position in the current row it copies to is offset by the row number, so it starts one place later for each row. The % operator is used to wrap the index back around to the start of the row when it gets to the end.
I'm not following your logic in the line
A[x][y] = 1*x + y;
I seem to have gotten closer using %:
A[x][y] = (1+x + y) % 10;
This takes the remainder of the sum of x and y when divided by 10 - which, if we start with 1 rather than 0, begins to approach what you want. It's not exactly correct, but it's a start.
=== Original matrix ===
1 2 3 4 5 6 7 8 9 0
2 3 4 5 6 7 8 9 0 1
3 4 5 6 7 8 9 0 1 2
4 5 6 7 8 9 0 1 2 3
5 6 7 8 9 0 1 2 3 4
6 7 8 9 0 1 2 3 4 5
7 8 9 0 1 2 3 4 5 6
8 9 0 1 2 3 4 5 6 7
Here's the code:
#include <stdio.h>
#define ROWS 8
#define COLS 10
int main(int argc, char* argv[]) {
int A[ROWS][COLS];
int B[COLS][ROWS];
int x,y;
for (x=0; x<ROWS; x++) {
for (y=0; y<COLS; y++) {
A[x][y] = (1+x + y) % 10;
}
}
printf("=== Original matrix === \n");
for (x=0; x<ROWS; x++) {
for (y=0; y<COLS; y++) {
printf("%3d ", A[x][y]);
}
printf("\n");
}
}

Resources