This is an exercise for C programming?it is Better to answer it with some explan
ID: 3902973 • Letter: T
Question
This is an exercise for C programming?it is Better to answer it with some explanations?Thanks!
Codes are as follow:
1.
// lab3exe_E.c
#include "lab3exe_E.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(void) {
Bits_Pattern bits_array[MAX_ARRAY_SIZE];
int size = read_bit_patterns(bits_array,"lab3exe_E.txt");
display_bit_patterns(bits_array, size);
fprintf (stdout, " Program terminated. ");
return 0;
}
int read_bit_patterns(Bits_Pattern * arr, const char* filename){
// in you AR diagram you don't need to show where FILE pointer, fp, points to.
// just show the arrow points to a box with a couple of question marks,
// anywhere besid your diagram.
FILE *fp;
int row = 0, col;
fp = fopen(filename, "r");
if(fp == NULL){
fprintf( stderr, "Error: cannot open file: %s", filename);
exit(1);
}
char c = fgetc(fp);
while (c != EOF && row < MAX_ARRAY_SIZE){
col = 0;
while(c != ' ' && c != EOF){
if(c != '0' && c != '1')
{
fprintf(stderr, "Error: Invlide character in: %s", filename);
exit(1);
}
arr[row].bits[col] = c;
col++;
c = fgetc(fp);
}
arr[row].bits[col] ='';
calculate_value(&arr[row]);
row++;
if(c != EOF)
c = fgetc(fp);
}
fclose (fp);
return row;
}
void calculate_value(Bits_Pattern * bits){
int position = 0;
bits->value =0;
for ( int i = (int) strlen(bits->bits)-1; i >= 0; i-- ) {
bits ->value = bits->value + (bits->bits[i]-48) * pow(2.00, position);
position++;
}
// POINT ONE - DRAW AR DIAGRAM AT THIS POINT WHEN REACHES FOR THE SECOND TIME
}
void display_bit_patterns(Bits_Pattern arr[], int size){
for(int i = 0; i < size; i++) {
for( int j = 0; arr[i].bits[j] != ''; j++)
fprintf(stdout, "%c", arr[i].bits[j]);
fprintf(stdout, " %12d ", arr[i].value);
}
}
2.
// lab3exe_E.h
#ifndef lab3ExE_h
#define lab3ExE_h
#define MAX_BITS 5
#define MAX_ARRAY_SIZE 4
typedef struct Bits_Pattern
{
char bits[MAX_BITS];
int value;
} Bits_Pattern;
int read_bit_patterns(Bits_Pattern * arr, const char* filename);
void display_bit_patterns(Bits_Pattern arr[], int size);
void calculate_value(Bits_Pattern * byte);
#endif /* lab3ExE_h */
3.
Lab3exe_E.txt
0011
0101
1111
1001
Codes are as above, there are three files.
Explanation / Answer
Your code looks fine and it is good explained please run if there is a logical error then ask otherwise all thing is fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.