C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Are there no proper initialising of array for the infitite loop? Posted by tokoG on 1 Feb 2006 at 8:26 PM
This message was edited by tokoG at 2006-2-1 20:27:45

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 properly initialise so that I can end the programme by ENTER and won't get the pop-up msg?

Thanks!!

#include<stdio.h>

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("\n\nNumbers in reverse order\n");
 for (i=length; i >= 0; i--)
    printf("%d ", numbers[i]);

 getchar();
}



Report
Re: Are there no proper initialising of array for the infitite loop? Posted by Gregry2 on 1 Feb 2006 at 10:10 PM
: This message was edited by tokoG at 2006-2-1 20:27:45

: 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 properly initialise so that I can end the programme by ENTER and won't get the pop-up msg?
:
: Thanks!!
:
:
: #include<stdio.h>
: 
: 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("\n\nNumbers in reverse order\n");
:  for (i=length; i >= 0; i--)
:     printf("%d ", numbers[i]);
: 
:  getchar();
: }
: 

:
:
:


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

Report
Re: Are there no proper initialising of array for the infitite loop? Posted by PooBear on 1 Feb 2006 at 10:25 PM
You can use STL vectors that will solve your problem or use dynamic arrays.
Report
Re: Are there no proper initialising of array for the infitite loop? Posted by tokoG on 2 Feb 2006 at 12:31 AM
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?
Report
Re: Are there no proper initialising of array for the infitite loop? Posted by stephl on 2 Feb 2006 at 3:05 AM
: This message was edited by tokoG at 2006-2-1 20:27:45

: 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 properly initialise so that I can end the programme by ENTER and won't get the pop-up msg?
:
: Thanks!!
:
:
: #include<stdio.h>
: 
: 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("\n\nNumbers in reverse order\n");
:  for (i=length; i >= 0; i--)
:     printf("%d ", numbers[i]);
: 
:  getchar();
: }
: 

:
I think a good way to achieve such a task would be to use a dynamic list, instead of resizing an array.

#include <stdio.h>
#include <stdlib.h>


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\n",pop(&stack));
 return 0;
}


Steph
Report
Re: Are there no proper initialising of array for the infitite loop? Posted by stober on 2 Feb 2006 at 6:13 AM
uinsg std::vector is a lot simpler
#include <vector>
using namespace std;

// main ALWAYS returns an int, never void 
int main () {
 
   vector<int> numbers;
   int i=0, length, n;
 
  while (1) {
   printf("Input an integer and use -1 to end: ");
   scanf("%d", &n);
//  fflush(stdin); // NO!  This is not standard!
    if (n == -1)
        break;
    i++;
    numbers.push_back(n);
 
  }

  length = numbers.size();
  printf("\n\nNumbers in reverse order\n");
  for (i=(length-1); i >= 0; i++)
     printf("%d ", numbers[i]);
 
  getchar();
 }





Report
Re: Are there no proper initialising of array for the infitite loop? Posted by stephl on 2 Feb 2006 at 6:52 AM
: uinsg std::vector is a lot simpler
:
: #include <vector>
: using namespace std;
: 
: // main ALWAYS returns an int, never void 
: int main () {
:  
:    vector<int> numbers;
:    int i=0, length, n;
:  
:   while (1) {
:    printf("Input an integer and use -1 to end: ");
:    scanf("%d", &n);
: //  fflush(stdin); // NO!  This is not standard!
:     if (n == -1)
:         break;
:     i++;
:     numbers.push_back(n);
:  
:   }
: 
:   length = numbers.size();
:   printf("\n\nNumbers in reverse order\n");
:   for (i=(length-1); i >= 0; i++)
:      printf("%d ", numbers[i]);
:  
:   getchar();
:  }
: 

:
:
Yes, you're right. However I just know C

Steph
Report
Re: Are there no proper initialising of array for the infitite loop? Posted by tokoG on 2 Feb 2006 at 7:29 PM
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
Report
Re: Are there no proper initialising of array for the infitite loop? Posted by Donotalo on 2 Feb 2006 at 9:13 PM
This message was edited by Donotalo at 2006-2-2 21:15:39

Add the blue lines.

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


~Donotalo()



Report
Re: Are there no proper initialising of array for the infitite loop? Posted by stephl on 3 Feb 2006 at 2:07 AM
: 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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.