reverse order intergers

Hello,
I'm in need of some little help.
I need lines of output are in reverse order. Define and use a function that accepts a single value x and prints a line containing the integers from −x to x. The newline that must be printed at the end of each line of output must be printed by the printing function.

Example 1:
$ ./a
3
2 0 4
-4 -3 -2 -1 0 1 2 3 4
0
-2 -1 0 1 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <stdio.h>

void Order (int a, int b, int c, int d) {
   while(a > b) {
         scanf("%d", &c);
         if(c >= 0) {
            d = -c;
            while(c >= d) {
               printf("%d ", d);
               d++;
            }
         }
         else {
            d = -c;
            while(c <= d) {
               printf("%d ", -d);
               d--;
            }
         }
         printf("\n");
         b++;
   }
}

int main (void) {
   int n;
   int w;
   int x;
   int y;

   scanf("%d", &x);

   n = 0;
   if(x >=0) {
      Order (x, n, y, w);
      return 0;
   }
     else{
        printf("Error: Zero or More Data Values Must be Provided.");
        return 1;
     }
}


My output:
$ ./a
3
2 0 4
-2 -1 0 1 2
0
-4 -3 -2 -1 0 1 2 3 4
Last edited on
Your function order should accept a SINGLE value, now you're passing in 4 arguments. The correct way to do this is:
1
2
3
4
5
6
7
8
9
void Order(int x)
{
    int a = -x;
    while(a <= x)
    {
        std::cout << a; //Or use printf("%d", a) if you want
        ++a;
    }
}
Not really what I had asked for... but thanks for trying to help.

Still looking for help
Last edited on
Explain what you need more clearly, @goldenchiken's answer does exactly what was described


Define and use a function that accepts a single value x and prints a line containing the integers from −x to x. The newline that must be printed at the end of each line of output must be printed by the printing function.


We really can't help you unless we understand the problem better.
Read an integer n from the keyboard.
Read an additional n integers from the keyboard. (I.e. n was the number of integers to follow).
For each of the n values (x), print the integers −x to x on a separate line.
It is an error if n < 0. In this case, print an error message and return a status of 1.

Which I have done already which is above, but I need to reverse the intergers

Example:
$ ./a
3
2
0
4
-4 -3 -2 -1 0 1 2 3 4
0
-2 -1 0 1 2
Hmm.. it looks like it prints out the values in the order they were input. I just ran the code you supplied and got the output matching the example (correct order). The code looks like it should produce the expected results. Is there a chance that the value are being input in a different order?

I will mention You should not need to pass 4 integer values to your order function. You could pass one, but you can absolutely cut out 2 of them. c and d can both be local to the function Order() (declare them at the top of the function) as they are assigned and used only within the function. You are passing in uninitialized variables right now (y,w) which is dangerous.
Think, what would Bjourne do?

~~~!!!HAPPY HALLOWEEN!!~~~~
Last edited on
That is what I need help with. Not to sure how to approach it. I need a program to do that but in reverse

After reading n, create an array of integers of size n.
Read the n integers from the keyboard and place them in the array.
Use counter controlled iteration to iterate over the elements of the array, from last to first.
For each element of the array (x), call a function that accepts a single integer value and prints
the integers −x to x followed by a newline.
@Ex0d Oh, Sorry I misunderstood your problem. I believe the easiest way would be to look into using an std::array and the reverse iterator (rend() and rbgin()) The it will be easy to traverse your array backwards. Here is the reference page for std::array http://www.cplusplus.com/reference/array/array/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "std_lib_facilities.h"
// #include <iostream> 
// using namespace std; (((( you'll probably need this and comment the std_lib_facilities )))

int main()
{
    int array_size; // this will be our number
    cout << "Enter a value for num1: ";
    cin >> array_size;

    int my_array[array_size]; // make our array now 
    int double_array = array_size * 2; // double our array so we can gain our negatives 
    int negative_size = array_size - double_array; // set a negative size ex: -5 

    for(int i = 0; i < double_array; i++) // loop twice the amount of times ( for negatives )
    {
        my_array[i] = negative_size; // element[0] = negative value 
        negative_size++; // increment 

        cout << my_array[i] << '\n'; // output the values 
    }
    cout << negative_size; // output that last variable 
}


Probably the worst way to do it but its simple and concise.

Example input = 5

We double the value to 10, so we can get the negative version by going , (5 - 10) = -5
then I increment until positive 5. That's about it.
Last edited on
Push your integers onto a stack.
Then pop them back off LIFO and use @goldenchicken's function to print out each line.
Topic archived. No new replies allowed.