Language exercise C [closed]

-5

Create a program that reads six integer values from the keyboard, and then displays values read in the reverse order on the screen. Inverse order would not be from the smallest to the smallest. How to do?

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

int main()
{
   int i, num[6];
   printf("Digite 6 numeros inteiros.\n");
   for(i=0; i<6; i++) {
        printf("Digite o %d valor: ", (i+1));
    scanf("%d", &num[i]);
   }
   system("cls");
   for(i=0; i<6; i++) {

   system("pause");
   return 0;
}

It's not homework, it's the book.

    
asked by anonymous 23.07.2015 / 02:39

4 answers

4

My reverse order interpretation would be as follows:

User enters the values: 1 2 3 4 5 6 The output would be: 6 5 4 3 2 1

The code looks like this:

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

int main()
{
   int i, num[6];
   printf("Digite 6 numeros inteiros.\n");

   for(i=0; i<6; i++) {
    printf("Digite o %d valor: ", (i+1));
    scanf("%d", &num[i]);
   }

   printf("Resultado:\n");
   for(i=5; i>=0; i--) {
    printf("%d\n", num[i]);
   }
   return 0;
}
    
23.07.2015 / 02:53
3

First, your example does not compile because a } is missing. Look how it looks when properly indented:

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

int main()
{
    int i, num[6];
    printf("Digite 6 numeros inteiros.\n");
    for (i = 0; i < 6; i++) {
        printf("Digite o %d valor: ", i + 1);
        scanf("%d", &num[i]);
    }
    system("cls");
    for (i = 0; i < 6; i++) {
       system("pause");
       return 0;
    }

I suppose you have forgotten printf and } before system("pause"); . So this would be the code (printing in direct order):

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

int main()
{
    int i, num[6];
    printf("Digite 6 numeros inteiros.\n");
    for (i = 0; i < 6; i++) {
        printf("Digite o %d valor: ", i + 1);
        scanf("%d", &num[i]);
    }
    system("cls");
    for (i = 0; i < 6; i++) {
        printf("O %d valor eh: ", num[i]);
    }
    system("pause");
    return 0;
}

You are reading the 6 numbers correctly. Then the solution would be simple. I have two alternatives, choose the one that suits you best.

Alternative 1:

Just print the numbers on the screen in reverse order. For this, you iterate the array (in the second for ) in reverse order:

for (i = 5; i >= 0; i--) {

Alternative 2:

You iterate the array in the correct order, but it fills it in the reverse order:

    scanf("%d", &num[5 - i]);
    
23.07.2015 / 14:54
0

A recursive solution: -)

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

void le_imprime(int n);

int main(void) {
    puts("Digite 6 numeros inteiros.");
    le_imprime(6);
    puts("");
    return 0;
}

void le_imprime(int n) {
    int a;
    if (!n) return;
    if (scanf("%d", &a) != 1) {
        fprintf(stderr, "Erro de leitura.\nPrograma abortado.\n");
        exit(EXIT_FAILURE);
    }
    le_imprime(n - 1);
    printf("%d ", a);
}
    
23.07.2015 / 10:05
-3

A pornographic solution (do not show it to anyone):

#include <stdio.h>

int main(int argc, char**allwaysnull) { int c;
  if( argc == 8 ){ return 0; }
  if( argc == 1 ){ printf("6 numeros inteiros.\n"); return main(2,NULL); }
  scanf("%d",&c);
  main(argc + 1,NULL); 
  printf("%d\n",c);
}

Update: By purist suggestion from @pmg, I've added the second main argument to (fraudulently) silence the -Wmain warning. The new version continues to be admittedly pornographic.

    
23.07.2015 / 14:39