Thread: Help with matrix this question

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    16

    Post Help with matrix this question

    Help with matrix this question-untitled-jpgHi guys, I really need your help for solving this problematic quiz the case is I need to make my own customized matrix but I'm stuck and here is what is have coded, this is my attachment :

    prob-42.pdf
    Code:
    #include <stdio.h>
    
    int main(){
        int m, n;
        scanf("%d %d",&m,&n);
        char matrix [30] [30];
    
        for(int i=0;i<m;i++)
            for (int j=0;j<n;n++)
                scanf("%s",&matrix[i][j]);
    printf("Your matrix result\n");
        for(int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                printf("%c", matrix[i][j]);
            printf("\n");
        }
    }
    Please help !
    Last edited by Audy Naufal; 10-19-2018 at 01:21 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    You have a bug on this line as you probably wanted to increment j, not n:
    Code:
    for (int j=0;j<n;n++)
    You have a bug on this line as matrix[i][j] is a char, not a string (or more accurately, an array of char used to store a string):
    Code:
    scanf("%s",&matrix[i][j]);
    From your printf call later, you probably wanted to use %c rather than %s.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    It looks like you didn't learn anything from your previous homework.

    "The first line will be an integer T, denoting the number of test cases".

    So what does your first line looking like this supposed to do?
    > scanf("%d %d",&m,&n);


    It's like you write the first thing that comes into your head, discover it doesn't work and come running to the forum for an answer on a plate.

    READ the question, break the problem down into simple single steps and build the program up one task at a time.

    Your first scanf should be a single %d reading into a variable called numTestCases (like I showed you here)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    16

    Post

    I'm sorry @Salem. I need help because im a newbie to c programming i ussualy works with HTML, css and javascript.
    I have an another question :

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    int main(){
     int A[1000000000],B[1000000000];
     int E,D;
     int i,j,c;
     int CaseNum,jawaban,answer;
     
     scanf("%d",&CaseNum);
     for(int i=0;i<=CaseNum;i++)
     {
      scanf("%d", &c);
      for(int j=0;j<c;j++)
      {
       jawaban = 0;
       for(j=0;j<c;j++)scanf("%lld", &A[j]);
       for(j=0;j<c;j++)scanf("%lld", &B[j]);
       
       for(int j=0;j<c;j++) answer[j]=A[j]*B[j];
      }
        for(j=0;j<c;j++) jawaban = jawaban;
        printf("Case #%d: %lld\n",i,j);
     }
     return 0;
    }
    I have a problem with the output that says :
    Help with matrix this question-capture-png
    and the program complains about invalid int[int] for array subscription that refers to this line of code :
    Code:
    for(int j=0;j<c;j++) answer[j]=A[j]*B[j];
    can you solve it ?
    Attached Files Attached Files
    Last edited by Audy Naufal; 10-19-2018 at 06:59 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Everytime you declare int j it's a new j, which you do three times (i.e. either one or two too many -- you can keep either the main one at the top or the two separate ones in the loops).

    You didn't declare answer to be an array, so you can't use it as such.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    > int A[1000000000],B[1000000000];
    You've just used 8GB of stack space!

    Start with something comfortable to work with, like 10.

    > scanf("%d",&CaseNum);
    > for(int i=0;i<=CaseNum;i++)
    So if CaseNum is 1, how many times does this loop run?

    > for(j=0;j<c;j++)scanf("%lld", &A[j]);
    lld isn't for integers, it's for long long int's.


    > I need help because im a newbie to c programming i ussualy works with HTML, css and javascript.
    So?
    The same process you follow for creating things in those things (breaking things down, step-wise refinement, debugging and testing) still apply.

    Just stop lunging for the solution in one step and proceed slowly. You're just frustrating yourself with serial failure every time you do.
    For you, this means you add ONE line at a time, press compile and make sure it works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Matrix Question
    By CJM in forum C Programming
    Replies: 6
    Last Post: 12-03-2010, 12:18 AM
  3. matrix question
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 08-04-2004, 07:19 PM
  4. Matrix question
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-30-2003, 05:35 PM

Tags for this Thread