Write a C program to do the followings: You program should define three arrays s
ID: 3845918 • Letter: W
Question
Write a C program to do the followings:
You program should define three arrays such that Array1 has eight values: $01, $02, $03, $04, $05, $06, $07, $08 and Array2 has eight values: $11, $12, $13, $14, $15, $16, $17, $18, and Array3 has eight bytes reserved. The program reads DIP switches, then, does the followings:
If the readings are exactly %00001001 = $09, then, it will turn on LED3 and LED0, and copy the data from Array1 to Array3; after copying data, your program is done.
If the readings are exactly %10010000 = $90, then, it will turn on LED7 and LED4, and copy the data from Array2 to Array3; after copying data, your program is done.
If the readings are neither $09 nor $90, then, turn on all LEDs, and go back to read DIP switches repeatedly until one of the two correct readings above is available.
Requirements:
Your program must use loop so that your program can be easily modified to work for arrays with 30, 50 or even more elements instead of just 8 elements as they are now.
It is preferred but not required that a single function does the data transfer (or data copying) for both cases.
Note: The DIP switches on Dragon12-light boards are numbered 12345678 for bits 76543210.
Explanation / Answer
#include <stdio.h>
void copyArrays(int[],int[],int);
int main(){
int size=8;
int ar1[]={1,2,3,4,5,6,7,8},ar2[]={11,12,13,14,15,16,17,18},ar3[size];
int dipSwitch;
scanf("%d",&dipSwitch);
while(1){
if(dipSwitch%9==9){
//dipSwitch%00001001
printf("turn on LED3 and LED0 ");
copyArrays(ar1,ar2,size);
break;
}
else if(dipSwitch%144==90){
////dipSwitch%10010000
printf("turn on LED7 and LED4 ");
copyArrays(ar2,ar3,size);
break;
}
scanf("%d",&dipSwitch);
}
return 0;
}
void copyArrays(int *ar1,int *ar2,int size){
int i;
for(i=0;i<size;i++){
ar2[i] = ar1[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.