Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:59:59+00:00 2026-06-07T03:59:59+00:00

I am coding a c project, that needs that the user enters a N*N

  • 0

I am coding a c project, that needs that the user enters a N*N square of integers : that’s to say an input of N lines of N integers. The algo works fine.

Now I want the user to input N lines of N integers each of consecutive integers are separated by a space. Here, I don’t have the right usage of scanf, because I tried to declare integers array but I was not able to deal with the spacing.

I tried something like this, very unnatural and failing :

int i=0;
int j=0;

int N;
scanf("%d",&N);

char c[N][2*N-1];

while(i < N){
    scanf("%s",&c[i]);
    i++;
}

i=0;
j=0;
while (i<N){
    while (j<N){
        c[i][j]=c[i][2*j]-48;
        j++;
    }
    j=0;
    i++;
}

Can someone help ?

Best,
Newben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:00:00+00:00Added an answer on June 7, 2026 at 4:00 am

    If I understood what your original code was supposed to do then this code should actually do it (and print it out again just to prove it worked).

    You need to dynamically allocate the array since it’s variable size ( In C99 you could use variable sized arrays on the stack but that’s really a different discussion ).

    scanf will automatically ignore white-space between the integers (including spaces and new-lines) so you don’t need to parse that out manually.

    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        int i=0, j=0, N;
        int **c;
    
        scanf("%d",&N);
    
        c = malloc(N*sizeof(int*));
    
        for (i=0;i<N;i++)
        {
            c[i] = malloc(N*sizeof(int));
            for (j=0;j<N;j++)
            {
                scanf("%d",&c[i][j]);
            }
        }
    
        for (i=0;i<N;i++)
        {
            for (j=0;j<N;j++)
            {
                printf("%d ",c[i][j]);
            }
            printf("\n");
            free(c[i]);
        }
        free(c);
        return 0;
     }  
    

    C99 alternative without malloc/free for completeness (I’v never liked this C99 feature since there’s no way to check that the was/is enough space on the stack):

    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        int i=0, j=0, N;
    
        scanf("%d",&N);
    
        int c[N][N];
    
        for (i=0;i<N;i++)
        {
            for (j=0;j<N;j++)
            {
                scanf("%d",&c[i][j]);
            }
        }
    
        for (i=0;i<N;i++)
        {
            for (j=0;j<N;j++)
            {
                printf("%d ",c[i][j]);
            }
            printf("\n");
        }
        return 0;
     }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I started coding a project that uses sqlite and when I tried to
We have a project that needs to gather survey data. On one particular page
I'm creating a class that needs to parse user contact info to determine if
We're working in a Dynamic Data project that will handle entities coming from two
I was happily using VS2010 Premium version for a C/C++ coding project, and needed
Hi there Stackoverflowers! I was coding a project when I wondered which is the
We're coding a Java6 project under Eclipse Indigo and we currently have some compilations
I was coding on a project in VS2008 and I tried to hit CTR
I'm coding a Rails project in a Windows environment and using Eclipse (with the
I'm coding a c++ project in vim. I'd like to run a ctags command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.