0

I want to print every 2 Seconds a random music note on the terminal, to practices on my ocarina. The set of the music notes is (A, B, C, D, E, F and G), that is the code i need help with ,since i am new to coding.


  #include <stdio.h>
  #include <unistd.h>
   int main(){
              char A, B, C, D, E, F, G;
              char notes[7] = { A, B, C, D, E, F, G };

              while (1) {

                         printf("%c\n", notes);
                         sleep(2);

                         }
   }

I get this error when compiling.

warning:format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char*’ [-Wformat=] printf("%c\n", notes);

When i change the %c in printf to %s all i get in the terminal "@" every 2 Seconds, what am i doing wrong? for the other part of my question how to add the print random notes which code i need to add?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
badboys
  • 9
  • 2
  • 4
    Look up `srand` and `rand` – Ed Heal Oct 21 '17 at 17:43
  • 2
    Change `char notes[7] = { A, B, C, D, E, F, G };` to `char notes[] = { "ABCDEFG";` – Ed Heal Oct 21 '17 at 17:44
  • 1
    and once you figure out how to use rand, initialize a variable i which gets the random number between 0 and 6 access the array as notes[i] – Shaun F Oct 21 '17 at 17:47
  • any particular reason why you want this in `C`? Almost any other popular language makes this much more easy. – bolov Oct 21 '17 at 17:58
  • Pun unintended — Note that the variables A..G are uninitialized, so when you initialize the `notes` array with their values, you are using indeterminate values. This is not good. You really don't need the separate variables. You do need to use `rand()` — and probably `srand()` too, but only [call `srand()` once](http://stackoverflow.com/questions/7343833/) — or something equivalent to generate new values. You pass an array of characters (equivalently, pointer to `char`) to the `printf()`; you tell it you're going to pass a `char`. Things don't work well when you lie. – Jonathan Leffler Oct 21 '17 at 19:10
  • i am a engineering student , and this is first time i am studying programing, after passing the exam i will study on my own other language – badboys Oct 21 '17 at 21:42

1 Answers1

1

You do not need to declare char variables separately, just initialize your array with the notes you want.

Then, in order to take the first note, you would need to do notes[0], for the second notes[1], for the third notes[2], ..., and for the last notes[sizeOfArray - 1]. So, in general, you access the i-th element of an array by doing notes[i], where indexing starts from 0.

So, you cannot do printf("%c\n", notes);, since %c expects a single character, while notes is an array of characters (a sting you could say). If you use %s, it will print the whole string, but we do not want that.

If we wanted to print the first character of the array, we would do:

printf("%c\n", notes[0]);

Now, we want to random access the array, thus we do not want 0, but something random, which will be in the allowed range (we have to be careful not to go out of bounds). So, this would be something nice:

printf("%c\n", notes[randomIndex]);

You can replace that randomIndex, by a function call, that will return a random index, like the randomRange() I have bellow.

Example:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>

int randomRange(int min, int max)
{
    return ( rand() % ( max - min + 1 ) ) + min;
}

int main(void)
{
    srand( (unsigned int)time ( NULL ) );
    const int size = 7;
    char notes[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
    for(int i = 0; i < 5; ++i) {
        printf("%c\n", notes[randomRange(0, size - 1)]);
        sleep(2);
    }
    return 0;
}

Output:

D
F
A
G
G
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    thanks you helped me a lot, i will study all this codes ( that is the first time i see them ) – badboys Oct 21 '17 at 21:21