How do i do this??

Hi! I am new to arduino and C++ programming and our instructor wants us to make the following programs. I've been researching about syntax and codes but still I can't program properly. I only learned how to input an int number and display it as is by using the atoi code.

So here's the programs I have to make and are due next week. Take note that input and output must be a number, not a string or char, preferably an int or long. I can only tell how the program should be but I do not know how to code it. Please help..Thanks!

1.A program that will accept a byte from the keyboard. The program determines if the byte is even or an odd. If it is even, it will compute and display the square of that byte. if it is odd, it will compute and display the cube of that byte.

ex: if i input 2, it will display " 2 is even and the square is: (squareValue)"
if i input 3, it will display "3 is even and the cube is: (cubeValue)"

2.A program that will accept 4 bytes from the keyboard. The program determines which byte is the smallest, the biggest and computes and display them as well as their average.

ex: if I input number '1234' all at once, the program will read each byte from that int/long which is 1,2,3, and 4 individually and compares to each other to know which byte is the smallest and which byte is the biggest and will display as:

The smallest byte is: 1
The biggest byte is: 4

The program will then computes the average
averageVal=(1+2+3+4)/no.of bytes //no. of bytes will be 4, since the input number "1234" is 4 bytes

and display it as:

The average is: (averageVal)

3.A program that will accept 5 signed bytes (+ or - byte). The program will display the count of positive bytes and the count of negative bytes.

ex: if i input 4,-5,1,-3,2 individually,which is a number after the other.
The byte is: 4 //after i input 4, it will be displayed like this. Next input will be 5 and will be displayed also like this.This will continue
The byte is: -5 this will continue till there will be an input of 5 bytes.
The byte is: 1
The byte is: -3
The byte is: 2

The count of positive bytes is: 3 //since 4,1 and 2 are positive
The count of negative bytes is: 2 //since -5 and -3 are negative

4.A program that will accept a byte from the keyboard. The program count the number of 1's on that accepted byte and display the at the output.

Ex: if i input numbers "2311451" all at once, the program will read each byte from that int/long individually and computes how many 1's are there in that number.

The count of 1's is: 3 //since there are 3 1's in the '2311451' number.

This isn't a homework help-line.
Post your code, and we can comment on it.

AWOL:
This isn't a homework help-line.
Post your code, and we can comment on it.

I can only tell how the program should be but I do not know how to code it.

I only learned how to input an int number and display it as is by using the atoi code.

So, show us your code.

ok so this code will only print what you have input.

int input=0;
char *output;

void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
{
input=Serial.read();
*output=input;
input=atoi(output);

Serial.print("The input is:");
Serial.println(input);
}
}

You may want to talk to your instructor about that code - it is dangerous in ways that you don't understand.

if (input & 1) {
  // input is odd
} else {
  // input is even
}

AWOL:
You may want to talk to your instructor about that code - it is dangerous in ways that you don't understand.

if (input & 1) {

// input is odd
} else {
 // input is even
}

I don't understand if(input & 1). What does it do??
With regards to the code I posted, I do understand it.The only problem is that how am I going to code those 4 programs in my first post with certain conditions like the odd/even byte, smallest/biggest byte,count of +/- bytes and the count of 1's.

I don't understand if(input & 1). What does it do??

It tests (as the following comment suggests) to see if the value of the variable "input" is odd.

With regards to the code I posted, I do understand it.

No, you really don't.

The pointer "output" has the value zero.
So, when you write "*output = input;" you're writing the character you input to memory location zero.
What is stored at RAM address zero?
I don't know, but I do know that I don't know, and that I shouldn't do anything with memory I haven't been told about or own.
Then you call "atoi", which expects a null-terminated string.
Is there a null-terminated string at memory address zero?
Possibly.

AWOL:

I don't understand if(input & 1). What does it do??

It tests (as the following comment suggests) to see if the value of the variable "input" is odd.

With regards to the code I posted, I do understand it.

No, you really don't.

The pointer "output" has the value zero.
So, when you write "*output = input;" you're writing the character you input to memory location zero.
What is stored at RAM address zero?
I don't know, but I do know that I don't know, and that I shouldn't do anything with memory I haven't been told about or own.
Then you call "atoi", which expects a null-terminated string.
Is there a null-terminated string at memory address zero?
Possibly.

^ but that code works fine. But what you've just said makes me feel that the code seems not right. Can you help me rectify it? Can you suggest or give a code that works similarly like the program I got? Geez, I really need to know how to write a program properly.

^ but that code works fine

It does work fine - because of what you might think of as a loophole.
Address zero is r0, and the compiler allows you to use r0 as anything you like.
Address one is r1, which the compiler likes to give the value zero.
So your code works for all strings with a length of one character.
You got lucky on this one architecture, with this very narrow usage.

char output [2];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()>0) {
    output [0] = (char) Serial.read();
    output [1] = '\0';
    int input=atoi(output);
   
    Serial.print("The input is:");
    Serial.println(input);
  }
}
char output [2];    

output [0] = (char) Serial.read();
output [1] = '\0';

[/quote]

could you please explain what these mean and how these work?

Before, you had an uninitialised pointer called "output".
The compiler kindly initialised it to zero for you, and provided two bytes of storage that were in fact machine registers.

What the code I have given you does is to explicitly declare two bytes of memory.
I don't have to know exactly where they are, but I do trust the compiler not to put them somewhere where they could be overwritten by some other code.

output [0] = (char) Serial.read();

Calls the "Serial.read" function (which returns an "int") and casts the result to be a "char" and assigns that "char" to the first of the two bytes that I declared.

output [1] = '\0';

Assigns the "char" value zero to the second byte that I declared. You will see this referred to as a null-terminated string. "null" here simply means the value zero.

Now, "output" contains a valid C string, and it is safe to call "atoi" with that string.

AWOL:

output [0] = (char) Serial.read();

what is the significance of (char) in this code? What will happen if it is not there or if it is changed to (int)?
Another question is that does Serial.read() returns only int??

I'm sorry if I've lots of questions.

The "(char)" cast does not have to be there.
You can leave it out.
It is just included as a reminder that we are trying to squeeze a quart (a sixteen bit value) into a pint (eight bit variable) pot.

We're telling the compiler "yes, this is an odd thing to do, but trust me, I know what I'm doing, so don't warn me about it".

And yes, "Serial.read" only returns "int"s, although it doesn't always fill them right up to the brim.

can i do this ,I declared the size array of char output to be only 1? By doing so, I also removed the output[1]='\0' from your source code.

char output [1];

void setup()
{
Serial.begin(9600);
}

void loop()
{
if(Serial.available()>0) {
output [0] = Serial.read();
int input=atoi(output);

Serial.print("The input is:");
Serial.println(input);
}
}

No, you can't do that.
"atoi" expects a string, and the only valid string with one element would contain no useful information, just the null terminator.
If you want a three character string, you must supply an array with at least four elements, one more than the length you expect to store.

I see. But should it be always equal to '\0'?? I wonder why does it have a , why not use '0' instead?

I'd like to thank you for your help. I'll try to make the first program. :smiley:

why not use '0' instead?

Because '0' has the decimal value 48 (0x30) - it is the ASCII character zero.
You could just write 0, but convention has it that you write '\0' to show it is char value, and not an "int".

I'll try to make the first program.

With what I've told you about how to test odd/even, you're 99% there.

I'd like to ask how the & works in if (input & 1), . What does it do?

The "&" is the bit-wise AND operation.
Its output is one only if both inputs are one.

Truth table for AND
A B A&B
0 0 0
0 1 0
1 0 0
1 1 1

Odd numbers expressed in binary will all have their least-significant bit set:

x binary x & 1
0 000 000
1 001 001
2 010 000
3 011 001
4 100 000
5 101 001
6 110 000
7 111 001

so

if (x & 1) {
  //do ODD
} else { //do EVEN
}

says "AND the value x with 1 and if the result is non-zero, "do ODD", if the result is zero, "do EVEN""

AWOL:

The "&" is the bit-wise AND operation.

Does it mean that whenever I input a number (from 0-9) it will be automatically converted into binary and the '&" will read only the least-significant bit 1 and AND it to 1, just as (x & 1)?

I tried changing '&' to '&&' and whenever the input is even the program read it as an odd number and computes the cube of the number which should not be like that. What is the difference of those two?