Write a C program to store each element of an array ad[] of size 8 into a single variable named data. Each element of the array holds either a 0 or a 1. Element zero of the array should be bit zero of data, element 1 of the array should be bit 1 of data, etc. For example, if the array is:

Respuesta :

Answer:

The program in C is as follows:

#include <stdio.h>

int main(){

   char data[50];

   int ad [8];

int i;

for (i = 0; i < 8; i++) {  scanf("%d", &ad[i]);  }

   int j = 0;    

   for (i = 7; i >= 0; i--) {

       sprintf(&data[j], "%d", ad[i]);

       j++;

   }

printf("%s",data);

   return 0;

}

Explanation:

This declares data as a string

   char data[50];

This declares ad as an integer array

   int ad [8];

This declares i as a counter variable

int i;

This iterates from 0 to 7 to get input for each element of integer array ad

for (i = 0; i < 8; i++) {  scanf("%d", &ad[i]);  }

This declares and initializes j to 0, as a counter variable

   int j = 0;    

This iterates through array ad, backwards and appends each element to variable data

   for (i = 7; i >= 0; i--) {

       sprintf(&data[j], "%d", ad[i]);

       j++;     }

This prints data

printf("%s",data);

ACCESS MORE