Language exercise C

Asked

Viewed 5,886 times

-6

Create a program that reads six integer values from the keyboard and then show on the screen the read values in reverse order. In reverse order, it 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 from the book.

  • 4

    I think the reverse order is this: Input: 1 3 5 7 9 Exit: 9 7 5 3 1

  • Print using the for no array back to front: for(i=5; i>=0; i--){ num[i];

4 answers

4

My interpretation of the reverse order would be as follows:

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

The code can be 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;
}
  • 1

    The printf of the result you are showing the i when you should show the num[i] :)

  • Moreover the second for should be of 5 until 0 and not of 6 until 1.

3

First, your example does not compile, because it lacks a }. Look how he gets when properly devised:

#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’ve forgotten one printf and a } before the system("pause");. So that 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 you find 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 fill it in the reverse order:

    scanf("%d", &num[5 - i]);

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);
}

-3

A pornographic solution (not to show 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: at @pmg’s purist suggestion, I added the second main argument to (fraudulently) shut the warning -Wmain. The new version continues to be openly pornographic.

  • The signature of the function main must have 0 or 2 arguments: int main(void) or int main(int argc, char **argv). How do you not use the argv you can use NULL on your show: main(argc + 1, NULL).

  • @pmg, updated.

Browser other questions tagged

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