So I am trying to write a very basic computational program in Arduino, but I am brand new to programming and have no idea what I'm doing. I'm trying to write an Arduino sketch that prints the integers in increasing order from 1 to n and then in decreasing order from n-1 to 1 in the serial COM window, when the variable n=7. The result should be displayed in a row, as opposed to a column, with at least two spaces in between each number.
This is what the program should spit out:
1 2 3 4 5 6 7 6 5 4 3 2 1
....I have no idea where to even start. I tried to make some basic logic statements to just get numbers 1 through 7 printed, but I know there must be an easier way, and I'm not sure how I would print the result in a row. This is what I have right now, but the right numbers aren't even being printed :/....can someone please give me a nudge in the right direction?
void setup() {
Serial.begin(9600);
int n = 7;
int x = n-6;
int y = x++;
int z = y++;
int a = z++;
int b = a++;
int c = b++;
If this does not show up Hello World in Your Serial monitor window then there first is some basic setup that needs to be worked out.
In your program you use Serial.println() - if you want things to stay on the same line use Serial.print(). You need to space things out a little so include a Serial.print(' '); between each so you get a space. (Yes, you have to every thing yourselv in programming, specify every little detail)
Lastly I think your approach is novel. A new way to solve your challenge. Not efficient or elegant, but you found what should be a workable solution.
To get you steered in the right direction consider reducing the number of variables like
int n=1 ;
Serial.print(n);Serial.print(' ');
n++ ; // or the equivalent n += 1 ; or the equivalent n=n+1 ;
Serial.print(n);Serial.print(' ');
n += 1 ; // does the same thing as above
Serial.print(n);Serial.print(' ');
n=n+1 ; // does the same thing as above
Serial.print(n);Serial.print(' ');
n++ ; // and should generate the same code
milosaurus:
I'm trying to write an Arduino sketch that prints the integers in increasing order from 1 to n and then in decreasing order from n-1 to 1 in the serial COM window, when the variable n=7. The result should be displayed in a row, as opposed to a column, with at least two spaces in between each number.
I'm trying to write an Arduino sketch that prints the integers in increasing order from 1 to n and then in decreasing order from n-1 to 1 in the serial COM window, when the variable n=7. The result should be displayed in a row, as opposed to a column, with at least two spaces in between each number.
Two for loops - one incrementing from 1 to n, the other decrementing from n-1 to 1. Printing in a column is done with println(). Printing on a row is done with print().
You aren't even in the right forest, let alone barking up the right tree.
Make sure we get some credit when you turn in your homework.
You're not old enough, KeithRB. Tweaking a product to match benchmarks is an old trade trick. 8)
One instance I can think of offhand, is graphics cards for pc in the late 80s. The driver code shipping with the card would detect if a large number of rectangles was being sent. It then stopped updating the display (thus accepting rectangle graphics commands really quickly) until the command stream stopped and then it generated the last 50 it had received. The result: Graphic card X was capable of doing 100000 rectangles a second whilst all others only did 500 or 1200. (Of course, doing only 50 would have shown it was no faster). Benchmark tester and computer magazines started to use displaying excel sheets as a benchmark to stop that particular cheat.
I'm sure cheats have been done before I or computers were born, too.
??? That's weird.
Are you sure your terminal didn't truncate anything or something?
I'm not even sure how that output would be possible with the given code.