Infinity C loop array

Asked

Viewed 63 times

-3

In this example below, how do I print on the screen all values assigned to n during the loop, after its completion?

#include <stdio.h>
#include <conio.h>
int main (void)
{
  int n;
  
  for (;;)
  {
    printf("Digite um numero inteiro: ");
    scanf("%d", &n);
    
    if (n == 7)
    {
      break; 
    }
    
    printf("Numero: %d\n",n);
  }
  
 
  getch();
  return(0);
} 

I tried to do so:

#include <stdio.h>
#include <conio.h>
int main (void)
{
  int n[30];
  
  for (;;)
  {
    printf("Digite um numero inteiro: ");
    scanf("%d", &n[i]);
    
    if (n == 7)
    {
      break; 
    }
    
    for (xxxxxx)
    printf("Numero: %d\n", n[i]);
  }
  
 
  getch();
  return(0);
}

I don’t know what’s inside "for(xxxxxx)".

2 answers

3

First your loop should not be infinite if you are reading values for the array "n", which has 30 positions, this should be the size/duration/number of interactions of your loop.

Some considerations:

  • If you declare an array with X positions, it makes no sense to read "part" of it and exit in the middle using the break, would be better to use a dynamic array, but most likely by your code and knowledge that apparently you have, it’s not what you need. For an array in c, use an array with defined size, unless it is extraordinarily necessary to do something different;
  • To conditions where we don’t know the number of interactions but only the stopping condition, it is better to use for example while. So in the first code the for (;;) could be while (n != 7), to exemplify;
  • Answering your question, to list the values just repeat the same loop below and show the values. This as an exercise, because if you just want to show, it can be in the same loop;

Let’s assume that the value 30, which you have in your condition is the limit of your loop, IE, you want to enter with 30 values, could do so (answering your question "I don’t know what’s inside "for(xxxxxx)":

int totalDeNumeros = 30;
int n[totalDeNumeros];

for (int i=0;i< totalDeNumeros;i++)
{
    printf("Digite um numero inteiro: ");
    scanf("%d", &n[i]);
}

printf("Numeros digitados:\n");
for (int i=0;i< totalDeNumeros;i++)
{
    printf("%d\n",n[i]);
}

About a dynamic array, have another question related to code example to realize that it is much more complex to implement: How to write a loop to read a file to a C struct array?

-5


Here’s what I’d do

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int n[30], i = 0, j;

    //lendo os valores  

    for( ; ; )
    {
        printf("Digite um numero inteiro: ");
        scanf("%d", &n[i]);
        if (n[i] == 7)
        {
           break; 
        }
        i++;
    }
    i++;

    //escrevendo após parar o loop

   for(j = 0; j < i; j++)
   {
       printf(" %d valor digitado: %d\n", j, n[j]);
   }
   getch();
   return 0;
}

You use a counter, in case i, to read and at the end it will save the position of the last entered value. Then you break the read loop and start a new one to write. It writes the read values by counting the positions with a second counter that goes from the first position of the array to the last that is stored in variable i.

  • Thanks a lot, bro. This methodology works fine in the code I was making.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.