Thread: Array Help Asap Please!!!!!!!!

  1. #1
    Unregistered
    Guest

    Angry Array Help Asap Please!!!!!!!!

    I have a problem. I have a program that defines an int a[20] so that I can put 20 different values into the array. I am writing a program that will ask the user to input integers and it can be anywhere from 1 - 20 integers. I can read in the values and print them but since my loop says this:

    printf("Enter random integers (max 20): "); (example) 2 3 4 2 42
    int i = 0;
    while (i < 19)
    {
    scanf("%d", a[i]);
    printf("%d ", a[i]);
    i++;
    }

    I get stuck in the never ending loop since the value may never reach nineteen. I do not know what to do because this will output the values but now I am stuck. All I need to do is just get the values from the one line of output to be written in the array at those memory locations and get out of the loop so I can finish the rest of my program.

    HELp!!!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Thumbs up Try if it works!!!!

    Okkkkkkk
    If the problem is with the scanf() then why we are not changing the function to take an integer from the user........
    Try that..........

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h> //for atoi() function

    int main(void)
    {
    char* strnum;
    clrscr(); //clears the screen
    int unsigned i[19]; //declaring an array of 20 elements

    for(int j=0;j<=19;j++) //assigning 0 to all the elements.....
    i[j]=0;

    printf ("\n Enter 20 random numbers of type integer \n");

    for(int k=0;k<=19;k++) {
    printf ("\n");
    gets(strnum); //getting the numbers as string
    i[k]=atoi(strnum); //converting the string to integer
    };


    for(k=0;k<=19;k++) {
    printf ("The value of element %d is %d \n", k, i[k] );
    };

    getch();
    return 0;
    };
    Last edited by rahat; 10-25-2001 at 01:28 PM.
    RaHaTk

  3. #3
    Unregistered
    Guest
    See I would do that but I have to put them in as an integer and they do not have to put all 20 integers in so my loop is becoming never ending. I would put them in as a string and cat the string but I can not do that. Please help me with my problem!!!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Thumbs down I think someone is missing something!!

    i[19] is containing 0 through 19............that means there are 20 elements all togather.....count it again and if you have confusions
    run the programme to varify if it works!

    The memory for the char* is undefiened but it wouldn't make too a great problem to carry on since we are working with a very simple programme.......

    The next is that....The problem is very simple and from that we can guess that the the man with that problem must be a new comer to C..... I am confident that you understand!!! So, a man who first wants to know how a loop works and terminates, I should not explain the mem.h to him and the ofcourse not the malloc or realloc.

    I have an idea......I think....and what i think is ......you know!
    RaHaTk

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    string

    you might want to try not initializing the string
    example
    str[];
    then after you get the string, by using the gets((str)string)
    the you can make the interger that you are going to use to start
    your loop a zero which means to append a null statement to the array as a termintor;
    ex. i=0;
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > you might want to try not initializing the string
    > example
    > str[];
    > then after you get the string, by using the gets((str)string)
    > the you can make the interger that you are going to use to
    > start your loop a zero which means to append a null statement
    > to the array as a termintor;
    > ex. i=0;

    Um no. This is very wrong.

    char str[]; /** not valid **/

    Even if it was:

    fgets( str, someRandomSize, someFile );

    This would kill your program because it'd be the same thing as usign an uninitalized pointer in the place of 'str'.

    Quzah.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Well I guess I was wrong

    hey kid I guess I was wrong because he seems to know what he is talking about, listen to quzah, I am a beginner to C myself. I thought I worked even though I hadn't tried, I did do something like it though.
    Last edited by incognito; 10-25-2001 at 01:59 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nah, you're doing fine. You can only use unsized arrays when you are initializing them at the time of declaration:

    char array[] = "My unsized array is being initialized now.";

    The same goes for "tables" (arrays of structures):
    Code:
    struct mystruct {
       int x;
       char *name;
    };
    
    struct mystruct  myTable[] =
    {
       { 1, "one" },
       { 2, "two" },
       { 3, "three" },
       { -1, "null" }
    };
    Quzah.

  9. #9
    Unregistered
    Guest

    coding as of now I can not use struct

    All right this is what I have right now. The only problem is go always = 1. So the loop still does not end and I am stuck at the very same place that I started at.

    PLEASE HELP



    #include <stdio.h>

    int a[20], i, n;

    int main()
    {
    int flag;
    int go;
    i=0;
    printf("Enter in integers (Max 20): ");
    while (i < 20)
    {
    a[i] = -1;
    i++;
    }

    i = 0;
    go = 1;
    while(go == 1)
    {
    flag=a[i];
    scanf("%d", &n);
    a[i]=n;
    printf("%d\n", a[i]);
    if(flag!=a[i])
    {
    go=1;
    i++;
    }
    else
    {
    go=0;
    }

    }
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Talking Funny.........

    „« i[19] is containing 0 through 19
    So int i[1] has 2 elements does it
    i[0] and i[1]
    Rubbish!!!

    „« hahahaˇKˇKˇK..
    I[0] and I[1] are two elements and that is FINAL.
    You go and read your books againˇK Try to be taught when you try to teach.

    Of course I was wrong in the sense that i[19] contains 20 elements. And thanks for all you have done.
    RaHaTk

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    okey, i am confused.

    If i do

    int elements[4];

    have i got 3 or 4 elements to use. I am pretty sure i actually got 4, as in zero to three but...

    Cheers
    G'n'R

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, you have four elements if you do this:

    int array[4];

    However, if you are trying to use a _string_, then in essence, you only get 3 due to the NULL at the end:

    char array[4] ;

    Quzah.

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    yep, gotch ya!

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    and... you access element 4 with elements[3]

  15. #15
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    yep gotcha again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Help Needed Asap With Searching An Array!
    By HelpMe++ in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2003, 04:44 AM