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.
#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;
}
}
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
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.
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/
#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.