#Using scanf function in my code

20 messages · Page 1 of 1 (latest)

vital forge
#

I am a beginner and need help in adding scanf feature in my code

Here is the code :

// Create an array of size 3 x 10 containing multiplication tables of the numbers 2,7 and 9 respectively.

#include <stdio.h>

int t[3][10], n[] = {2, 7, 9}; // t is a 3 x 10 array and n is an array of size 3

void fill(int i, int j) {
if (i == 3) return; // Index out of bounds
t[i][j] = n[i] * (j + 1);
fill(i + (j == 9), (j + 1) % 10);
}

void print(int i, int j) {
if (i == 3) return;
if (j == 0) printf("Table of %d:\n", n[i]);
printf("%d x %d = %d\n", n[i], j + 1, t[i][j]);
print(i + (j == 9), (j + 1) % 10);
}

int main() {
fill(0, 0);
print(0, 0);
return 0;
}

I wanted to make the code as shortest as possible therefore i have used recursion, and now i want to advance it by using scanf function to take input from the user.
I’d really appreciate help on how to:

  • Replace the hardcoded n[] with user input using scanf.
  • Make sure the recursion still works with that change.
radiant remnantBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

radiant remnantBOT
#

I am a beginner and need help in adding scanf feature in my code

Here is the code :

// Create an array of size 3 x 10 containing multiplication tables of the
// numbers 2,7 and 9 respectively.

#include <stdio.h>

int t[3][10],
    n[] = {2, 7, 9};  // t is a 3 x 10 array and n is an array of size 3

void fill(int i, int j) {
  if (i == 3)
    return;  // Index out of bounds
  t[i][j] = n[i] * (j + 1);
  fill(i + (j == 9), (j + 1) % 10);
}

void print(int i, int j) {
  if (i == 3)
    return;
  if (j == 0)
    printf("Table of %d:\n", n[i]);
  printf("%d x %d = %d\n", n[i], j + 1, t[i][j]);
  print(i + (j == 9), (j + 1) % 10);
}

int main() {
  fill(0, 0);
  print(0, 0);
  return 0;
}

I wanted to make the code as shortest as possible therefore i have used recursion, and now i want to advance it by using scanf function to take input from the user.
I’d really appreciate help on how to:

  • Replace the hardcoded n[] with user input using scanf.
  • Make sure the recursion still works with that change.
Akemi
static grove
#

I wanted to make the code as shortest as possible therefore i have used recursion
that looks like a false presumption

#

plus makes your code quite hard to read and reason about

#

while it would be just three lines in function when used with nested for

#

it is really interesting thinking with your recursion here, I mean some algorithms are naturally comfortably presented by recursion, but this is an extremely reversed case :)

#

for is the most obvious choice for visiting an array

#

so my recommendation is to rewrite your logic into using loops, and then the functions can e.g. accept dimensions as input

#

and guessing that this isn't really code-golfing, then:

int t[3][10];
int n[] = {2, 7, 9};  // t is a 3 x 10 array and n is an array of size 3

is rather widely preferred style

vital forge
#

Yeah you’re totally right. I am new to coding and it’s been just a couple of weeks since I started learning C, so I’ve been experimenting with different styles and just playing around with recursion a bit.

#

like this?


#include <stdio.h>

#define ROWS 3
#define COLS 10

int main() {
    int t[ROWS][COLS];
    int n[ROWS];
    
    printf("Enter 3 numbers to generate their multiplication tables:\n");
    for (int i = 0; i < ROWS; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &n[i]);
    }

    // Fill the tables
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            t[i][j] = n[i] * (j + 1);
        }
    }

    // Print the tables
    for (int i = 0; i < ROWS; i++) {
        printf("\nTable of %d:\n", n[i]);
        for (int j = 0; j < COLS; j++) {
            printf("%d x %d = %d\n", n[i], j + 1, t[i][j]);
        }
    }

    return 0;
}```
desert vigil
#

You can post your code with highlighting using backticks '`'. Just modify the code you posted above.

vital forge
radiant remnantBOT
#

@vital forge Has your question been resolved? If so, type !solved :)

vital forge
vital forge
#

!solved