Thread: Whats the problem with my array?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Angry Whats the problem with my array?

    Wrong Output. Whats the problem with my program?

    Objective: Write a program in C that uses array to store the telephone no. of students against their roll no. Input a roll no, and then search the array for the student's telephone number.

    Code:
    #include<stdio.h>
    void main()
    {
           int arr[3][1];
           int i,j,k=100,roll;
    
           for(i=0,j=0; i<3; i++)
        {
        arr[i][j]=k;
        k++;
        }
    
        for(i=0,j=1; i<3; i++)
        {
        printf("\nEnter Phone no. Against Roll no. %d:  ", arr[i][j]);
        scanf("%d", &arr[i][j]);
        }
    
    
        printf("\nEnter Roll no. ");
        scanf("%d", &roll);
    
        for(i=0,j=0; i<3; i++)
            {
                if(roll==arr[i][j])
                {
                    for(i=0,j=1; i<3; i++)
                          printf("Phone No. %d", arr[i][j]);
                }
            }
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    first off
    Code:
    int main(void);
    second, your code is all over,
    third, your only inputting 3 inputs, when you need to input roll number, then phone number 3 times
    fourth your output is garbled and only outputting 3 things total, need to use \n

    what you need to do in the 2d array,
    i and j = number
    0 0
    1 1
    2 2
    so you get the roll number, followed by the phone number

    i would say make a 1d array
    use i as your roll number
    and j as your phone number....

    and go from there...

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    plz edit the code and post for me.thanks

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by shansajid View Post
    plz edit the code and post for me.thanks
    We do VERY little of that. We help - you do your own work.

  5. #5
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    i didnt get it, what do u want to making a program,
    please explain it with another way explanation, maybe i can help u making the code,

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    Quote Originally Posted by loserone+_+ View Post
    i didnt get it, what do u want to making a program,
    please explain it with another way explanation, maybe i can help u making the code,
    Objective: Write a program in C that uses array to store the telephone no. of students against their roll no. Input a roll no, and then search the array for the student's telephone number.

    plz make this with your experiance.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    >to store the telephone no. of students against their roll no.
    Could you give an example of that?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    example

    Roll no. Phone no.
    1001 +665214574
    1002 +665242344
    1003 +6634214574
    1004 +6356544534
    1005 +6432214574
    . .
    . .
    . .

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok, so every student can be instantiated as a struct with members the Roll no, and the Phone no.
    Then create an array of students(structs).
    Then take from input a roll no. and search the array for it.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Or if you don't want to use structs you could use two separates arrays, but in you mind you should always have that rollArray[0] corresponds to phoneArray[0], etc.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by shansajid
    example

    Roll no. Phone no.
    1001 +665214574
    1002 +665242344
    1003 +6634214574
    1004 +6356544534
    1005 +6432214574
    . .
    . .
    . .
    okay you want that
    so in this code here,
    Code:
    #include<stdio.h>
    void main()
    {
           int arr[3][1];
           int i,j,k=100,roll;
     
           for(i=0,j=0; i<3; i++)
        {
        arr[i][j]=k;
        k++;
        }
     
        for(i=0,j=1; i<3; i++)
        {
        printf("\nEnter Phone no. Against Roll no. %d:  ", arr[i][j]);
        scanf("%d", &arr[i][j]);
        }
     
     
        printf("\nEnter Roll no. ");
        scanf("%d", &roll);
     
        for(i=0,j=0; i<3; i++)
            {
                if(roll==arr[i][j])
                {
                    for(i=0,j=1; i<3; i++)
                          printf("Phone No. %d", arr[i][j]);
                }
            } 
    
    }
    what result do u want, i still didnt get it,
    is that roll hold the value of the number u giving with scanf?
    please be detailed on your questions,

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Don't reproduce false code loserone
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by std100093
    Don't reproduce false code loserone
    wahahaha, funny when i read thiss,,
    i didnt make the code, the code is from the thread owner
    i am asking what result he/she want to,, i still cant get it,
    std10093 u had a false read, i didnt make a false code, wahahaha :P
    Last edited by loserone+_+; 01-21-2013 at 07:49 AM.

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes I know that

    What I want to say is that, it would be nice to fix some obvious errors and post back
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    i will help,
    but i had to know what result he/she have
    what he/she expected from his giving code to his thread,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats the best way to clear an array?
    By Hitsuin in forum C++ Programming
    Replies: 15
    Last Post: 06-14-2009, 05:10 AM
  2. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  3. Compiling whats held in an array
    By brett in forum C Programming
    Replies: 4
    Last Post: 04-04-2008, 09:47 AM
  4. Whats wrong with my array?
    By ammochck21 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2006, 01:11 PM
  5. Whats wrong with my array?
    By Munkey01 in forum C++ Programming
    Replies: 23
    Last Post: 03-26-2003, 06:07 AM