Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Are there no proper initialising of array for the infitite loop?

tokoGtokoG Posts: 209Member
[b][red]This message was edited by tokoG at 2006-2-1 20:27:45[/red][/b][hr]
I've been asking this question on my school msg board but all the replies I am getting sound like if there is no proper way of initialising an array for the infinite loop for example as follows;

This programme I wrote below WORKS.
But it doesn't end the programme properly instead I get this 'Access Violation' pop-up msg.

If I want to make this programme infinite, how should I [red]properly[/red] initialise so that I can end the programme by ENTER and won't get the pop-up msg?

Thanks!!

[code]
#include

void main () {

int numbers[]={0}, i=0, length;

while (1) {
printf("Input an integer and use -1 to end: ");
scanf("%d", &numbers[i]);
fflush(stdin);
if (numbers[i] == -1)
break;
else
i++;

}

length = i-1;
printf("

Numbers in reverse order
");
for (i=length; i >= 0; i--)
printf("%d ", numbers[i]);

getchar();
}
[/code]


Comments

  • Gregry2Gregry2 Posts: 607Member
    : [b][red]This message was edited by tokoG at 2006-2-1 20:27:45[/red][/b][hr]
    : I've been asking this question on my school msg board but all the replies I am getting sound like if there is no proper way of initialising an array for the infinite loop for example as follows;
    :
    : This programme I wrote below WORKS.
    : But it doesn't end the programme properly instead I get this 'Access Violation' pop-up msg.
    :
    : If I want to make this programme infinite, how should I [red]properly[/red] initialise so that I can end the programme by ENTER and won't get the pop-up msg?
    :
    : Thanks!!
    :
    : [code]
    : #include
    :
    : void main () {
    :
    : int numbers[]={0}, i=0, length;
    :
    : while (1) {
    : printf("Input an integer and use -1 to end: ");
    : scanf("%d", &numbers[i]);
    : fflush(stdin);
    : if (numbers[i] == -1)
    : break;
    : else
    : i++;
    :
    : }
    :
    : length = i-1;
    : printf("

    Numbers in reverse order
    ");
    : for (i=length; i >= 0; i--)
    : printf("%d ", numbers[i]);
    :
    : getchar();
    : }
    : [/code]
    :
    :
    :

    [green]
    Hey

    Uh...an "infinite" length array is impossible, for there is a limit on the memory of the machine of course, but you could use dynamic allocation to get...more than you would know during runtime.

    So, gah...wait...oh yeah, do you know about dynamic allocation? (new or malloc() ?) It can be a vast subject, so you should look for tutorials for more info.

    I'm at school, so I can't give much of a long reply right now...sorry.

    later
    {2}rIng
    [/green]
  • PooBearPooBear Posts: 19Member
    You can use STL vectors that will solve your problem or use dynamic arrays.
  • tokoGtokoG Posts: 209Member
    Hi Gregry & PooBear

    Thanks for letting me know!(During your class time..?!)
    Too bad.. I am a biginner and just started learning programming from last October so I haven't got to that point yet.


    OK, for now I just forget and will follow my school text. :)
    But thanks for the reply.

    Gregry - just curious. What kind of class are you taking?
  • stephlstephl Posts: 422Member
    : [b][red]This message was edited by tokoG at 2006-2-1 20:27:45[/red][/b][hr]
    : I've been asking this question on my school msg board but all the replies I am getting sound like if there is no proper way of initialising an array for the infinite loop for example as follows;
    :
    : This programme I wrote below WORKS.
    : But it doesn't end the programme properly instead I get this 'Access Violation' pop-up msg.
    :
    : If I want to make this programme infinite, how should I [red]properly[/red] initialise so that I can end the programme by ENTER and won't get the pop-up msg?
    :
    : Thanks!!
    :
    : [code]
    : #include
    :
    : void main () {
    :
    : int numbers[]={0}, i=0, length;
    :
    : while (1) {
    : printf("Input an integer and use -1 to end: ");
    : scanf("%d", &numbers[i]);
    : fflush(stdin);
    : if (numbers[i] == -1)
    : break;
    : else
    : i++;
    :
    : }
    :
    : length = i-1;
    : printf("

    Numbers in reverse order
    ");
    : for (i=length; i >= 0; i--)
    : printf("%d ", numbers[i]);
    :
    : getchar();
    : }
    : [/code]
    :
    I think a good way to achieve such a task would be to use a dynamic list, instead of resizing an array.

    [code]
    #include
    #include


    typedef struct node_s
    {
    int intvalue;
    struct node_s *next;
    } NODE;


    typedef NODE *STACK;


    void push(STACK *stackp,int i);
    int pop(STACK *stackp);


    void push(STACK *stackp,int i)
    {
    NODE *newnode;

    if ((newnode=(NODE *) malloc(sizeof (NODE)))!=NULL)
    {
    newnode->intvalue=i;
    newnode->next=*stackp;
    *stackp=newnode;
    }
    }


    int pop(STACK *stackp)
    {
    int i;
    NODE *p;

    p=*stackp;
    *stackp=p->next;
    i=p->intvalue;
    free(p);
    return i;
    }


    int main(void)
    {
    int n;
    STACK stack=NULL;

    while (1)
    {
    printf("Input an integer and use -1 to end: ");
    scanf("%d",&n);
    if (n==-1) break;
    else push(&stack,n);
    }
    while (stack!=NULL) printf("%d
    ",pop(&stack));
    return 0;
    }
    [/code]

    Steph
  • stoberstober Posts: 9,765Member ✭✭✭
    uinsg std::vector is a lot simpler
    [code]
    #include
    using namespace std;

    [red]// main [green]ALWAYS[/green] returns an int, never void[/red]
    int main () {

    vector numbers;
    int i=0, length, n;

    while (1) {
    printf("Input an integer and use -1 to end: ");
    scanf("%d", &n);
    // fflush(stdin); [red]// NO! This is not standard![/red]
    if (n == -1)
    break;
    i++;
    numbers.push_back(n);

    }

    length = numbers.size();
    printf("

    Numbers in reverse order
    ");
    for (i=(length-1); i >= 0; i++)
    printf("%d ", numbers[i]);

    getchar();
    }
    [/code]




  • stephlstephl Posts: 422Member
    : uinsg std::vector is a lot simpler
    : [code]
    : #include
    : using namespace std;
    :
    : [red]// main [green]ALWAYS[/green] returns an int, never void[/red]
    : int main () {
    :
    : vector numbers;
    : int i=0, length, n;
    :
    : while (1) {
    : printf("Input an integer and use -1 to end: ");
    : scanf("%d", &n);
    : // fflush(stdin); [red]// NO! This is not standard![/red]
    : if (n == -1)
    : break;
    : i++;
    : numbers.push_back(n);
    :
    : }
    :
    : length = numbers.size();
    : printf("

    Numbers in reverse order
    ");
    : for (i=(length-1); i >= 0; i++)
    : printf("%d ", numbers[i]);
    :
    : getchar();
    : }
    : [/code]
    :
    :
    Yes, you're right. However I just know C :-)

    Steph
  • tokoGtokoG Posts: 209Member
    Hi

    Does this code you posted suppose to work on C++ Bulder?
    I tried but I got the strange msg. I am beginner and not familier with this type of msg. If you'd like to read it, I will post it though.

    xx

    tokoG
  • DonotaloDonotalo Posts: 715Member
    [b][red]This message was edited by Donotalo at 2006-2-2 21:15:39[/red][/b][hr]
    [purple]Add the blue lines.[/purple]

    : uinsg std::vector is a lot simpler
    : [code]
    [blue]#include [/blue]
    : #include
    : using namespace std;
    :
    : [red]// main [green]ALWAYS[/green] returns an int, never void[/red]
    : int main () {
    :
    : vector numbers;
    : int i=0, length, n;
    :
    : while (1) {
    : printf("Input an integer and use -1 to end: ");
    : scanf("%d", &n);
    : // fflush(stdin); [red]// NO! This is not standard![/red]
    : if (n == -1)
    : break;
    : i++;
    : numbers.push_back(n);
    :
    : }
    :
    : length = numbers.size();
    : printf("

    Numbers in reverse order
    ");
    : for (i=(length-1); i >= 0; [red]i--[/red])
    : printf("%d ", numbers[i]);
    :
    : getchar();
    [blue]return 0;[/blue]
    : }
    : [/code]
    [hr][purple]~Donotalo()[/purple]



  • stephlstephl Posts: 422Member
    : Hi
    :
    : Does this code you posted suppose to work on C++ Bulder?
    : I tried but I got the strange msg. I am beginner and not familier with this type of msg. If you'd like to read it, I will post it though.
    :
    : xx
    :
    : tokoG
    :
    Which code are you talking about? Stober's one or mine? Mine is written in C and was tested with Borland C Builder 5. Stober's code is in C++, and since I don't know C++, I cannot tell more :-). However, it comes from Stober so I think it should work.

    Steph
Sign In or Register to comment.