User:Tolga Ozuygur/Clapmusicinc
Aloha,
This is the clapping music in c assignment. It was actually a nested loop exercise, but I did not use any nested loop while coding it, instead I have used some sort of a counter mechanism. Consider it as -one- of the plethora of alternative ways of completing such task.
While compiling the following code in linux, you might want to remove:
#include <windows.h>
#include <winbase.h>
and add:
#include <unistd.h>
Here is the source:
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <winbase.h>
int chn_1_timer = 0;
int chn_2_timer = 0;
int chn_timer_limit = 12;
int switch_timer = 1;
int switch_timer_limit = 12;
int loop_active = 1;
char pat [] = "xxx xx x xx ";
int patlen;
int main () {
while (loop_active == 1) {
if (pat[chn_1_timer] =='x'){
printf ("%s ", "*clap*");
}else{
printf ("%s ", " ");
}
if (pat[chn_2_timer] =='x'){
printf ("%s \n", "*clap*");
}else{
printf ("%s \n", " ");
}
chn_1_timer++;
chn_2_timer++;
if(chn_1_timer >= chn_timer_limit){
switch_timer++;
if(switch_timer >= switch_timer_limit){
switch_timer = 1;
chn_2_timer++;
}
chn_1_timer = 0;
}
if(chn_2_timer >= chn_timer_limit){
chn_2_timer = chn_2_timer-chn_timer_limit;
}
if(chn_2_timer < 0){
chn_2_timer = chn_timer_limit-chn_2_timer;
}
Sleep(100);
}
}