Thread: matrix- exercise the use of arrays in the code.

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    32

    matrix- exercise the use of arrays in the code.

    hello,
    i'm a complete beginner in C programming and would love to get your help with somthing. a attached the assignment.

    thank you!!
    Attached Images Attached Images matrix- exercise the use of arrays in the code.-part-1-jpg matrix- exercise the use of arrays in the code.-part-2-jpg 

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It is better for you and for us to first post your code,your actually attempt in your assignment and then ask something based on this.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    it's sad, but the only thing i mannaged to do is this:


    if (choice==1)
    {
    printf("Enter matrix size:");
    scanf("%d",&matrix_size);
    while (matrix_size<1 || matrix_size>10)
    {
    printf( "**** Incorrect input ****\n");

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Not bad. I guess you have declared matrix_size as int.You should write your code like this
    [key]<YOUR CODE GOES HERE>[/key].Replace word key with in order this to work.

    So the problem you have is to how read a row of the array.You should read every line seperately,probably with fgets(search the web for reference and you find something like this fgets - C++ Reference).

    Or you should check something like this How to read a line from the console in C - Stack Overflow (found it by searching for 'how to read a line in c'.

    However i am hesitant to say more because the hw says that you should only use what you are taught in class,but we aren't in the same class :/

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    oh sorry. the things i learned: if, else if, while, do while, getchar, putchar, printf, scanf, for, %, >,<,==,&&,||,!,define, arrays,gets,puts.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I can not allow to myself to suggest you using gets.(possible overflow of your buffer,no good function)
    You could use getchar to read char by char the input and handle these with the ASCII codes.Of course no need to know them by heart.
    e.g.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int c;
        c=getchar();
        if(c=='5')
        {
            printf("ASCII code of input : %d\n",c);
            printf("Value of input : %c\n",c);
        }
        else
            printf("Nothing happens\n");
        return 0;
    }
    Of course this is not very convenient,but using gets is a bad concept.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    thank you so much. but i can't use return.. and we usely write void main, and not int main..does in matter ? what is buffer overflow ?
    somthing i didn't understand.. why c==5 ? why 5 specifically?
    can you explain what exactlly you did in hear ? this is after i write the array ? i do it using nested for ?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    thank you so much. but i can't use return.. and we usely write void main, and not int main..does in matter ?
    Yes! Using void main() is wrong. Every version of the C standard insists that main return an int.
    but i can't use return..
    What? The return statement is a required element of every program, according to the C standards main should return an int.
    what is buffer overflow ?
    A buffer overflow is when you attempt to read past the end of an array. And since gets() doesn't enforce any size limitations it is particularity susceptible to this problem.

    Jim

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    and if i wanted to use gets anyway? how would it would look like ?
    and alos, how do i make sure that there are only numbers in there and not letters and there not 0 ? and that the amout of numbers that the user put is the amout that they asked in the exercise?

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    really we always use void main and no return.. at least for now.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I used 5 for instance.Same logic will make you read 1,2,3 etc. .What i did there is to write a single character from the user.Then assign this value to variable c which is declared as a char.Then i just print the values.Mind that with %d,i print the ASCII code of 5,which is 53(no need to know these by heart) and with %c i printed the value of 5,which is five.

    As for the int main(void) and return 0 it is ok for some compilers.They do this by default.I remember that during the first weeks in DIT,we also used it the same way.No need to focus there now.Focus on your hw!

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    and if i wanted to use gets anyway? how would it would look like ?
    I you want to insist on learning something that is so dangerous, I suggest you try to find out yourself. I won't help you learn something that is so bad that the C standard has removed this function from the language.
    how do i make sure that there are only numbers in there and not letters and there not 0 ?
    You need to check each character to insure it is a digit.
    really we always use void main and no return.. at least for now.
    Then you are learning how not to write proper C programs, I suggest you ask your instructor why they are teaching you wrong!

    Jim

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    And in order to check if a number is not 0 you just check with
    [code]if(number>0)
    /*then we are ok*/
    else
    /*not ok*/[/code

    @Jim i am sure it is because they are at the start of it.In the near future the instructor will taught about how main is supposed to be written.

    As for the gets,it is not just me and jim that refuse(for your own good ) to show you how it works,but if you search the web many other people suggest never use gets!

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    SAMARAS is right.. we are just at the start.
    how do i cheak each charecter ti insure it is a digit ?

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The right thing is that you post your attempt as i said before.

    Hint:Check if the character is a number by using operators >,<,=,>=,<= to ensure that the character is at the range of a number.If it is not,then it not a number.Use it a loop in order to check all the characters.
    e.g.
    [code]if( character<='0' || character>='9')
    /*then you should figure out what must be done*/
    [/code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K & R exercise - which code would you prefer?
    By kkk in forum C Programming
    Replies: 18
    Last Post: 08-17-2011, 06:42 PM
  2. matrix addition program using arrays
    By suryak in forum C Programming
    Replies: 6
    Last Post: 04-10-2011, 06:37 AM
  3. Matrix Arrays
    By kris_perry2k in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 02:15 PM
  4. Matrix multiplication using 1-d arrays
    By Befuddled in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 09:35 PM
  5. Grids, Matrix, 2D Arrays...
    By [email protected] in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2002, 08:13 AM