Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone! In school we've been learning programming in C. Well i knew alot of programming before we learned it but still I'm stuck on a project we had to finish. We got 5 projects and i've completed 4 of them, really easy. We've just been learning the basics like loops, if, else, arrays and easy stuff which i already knew but in this project i have to scan 5 numbers and print them in the opposite order and i don't actually know how i can do this.. I have an example here but i wonder if there are any better ways to do it?

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

int main(int argc, char *argv[])
{
  int number1, number2, number3, number4, number5; //Numbers to input
  
  printf("Enter five numbers. First number: ");
  scanf("%d", &number1);
  printf("Second number: ");
  scanf("%d", &number2);
  printf("Third number: ");
  scanf("%d", &number3);
  printf("fourth number: ");
  scanf("%d", &number4);
  printf("fifth number: ");
  scanf("%d", &number5);
  
  printf("%d, %d, %d, %d, %d", number5, number4, number3, number2, number1);
  
  getch();	
  return 0;
}


It doesn't look very good done, does it? Could i use an array or something and do this in a better way? if so, how?
Posted
Comments
nv3 24-Mar-14 8:51am    
Exactly, use an array and a loop running from 0 ... 4 to read the number and a second loop from 4 ... 0 to print them.

You've mentioned one way of doing it for yourself!

Why not assign the numbers into an array as they are entered, then display them by starting at the end of the array and working backwards?
 
Share this answer
 
Comments
Gramas19 24-Mar-14 8:55am    
Yeah, but how can i start from the end? I don't know that :s
CHill60 24-Mar-14 9:25am    
Well there are ways of finding out the size of an array, so start your loop with that number and decrement your loop counter each time until it reaches 0. That link that Aarti has posted is really good for looking things like this p
Aarti Meswania 24-Mar-14 9:11am    
5!+ :)
CHill60 24-Mar-14 9:22am    
Thank you!
Aarti Meswania 24-Mar-14 9:25am    
Welcome! :)
http://www.cprogramming.com/tutorial/c/lesson8.html
check link there is a program with char array you can judge from that how do this with integer array

in that program values scanned in a loop and printed using a loop
this is advantage of using array it will reduce code.
no need to declare too many variables number1,number2...,number5 and also no need to write too many lines to scan

Happy Coding!
:)
 
Share this answer
 
Comments
Gramas19 24-Mar-14 9:31am    
I still don't get it, could you write it in code how you mean? I'm a bit unknowing about arrays so i have to learn it better :)
Aarti Meswania 24-Mar-14 9:41am    
sorry but you should do it your self I have already given explaination why array is used and also given a link to see an example
// simple array
C++
int main(int argc, char *argv[])
{
    int index;
    int number[5]; //Numbers to input
  
    printf("Enter five numbers\n");

    for (index = 0; index < 5; index++)
    {
        printf("Next number: ");
        scanf("%d", &number[index]);
    }

    for (index = 4; index > -1; index--)
    {
        printf("%d", number[index]);
        if (index > 0)
            printf(", ");
    }
    printf("\n");

    getch();	
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900