#include #include #include //starter code to finish: const unsigned UNS_MAX = -1;

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

#include #include #include //starter code to finish: const unsigned UNS_MAX = -1;

Post by answerhappygod »

Include Stdio H Include Stdbool H Include Stdlib H Starter Code To Finish Const Unsigned Uns Max 1 1
Include Stdio H Include Stdbool H Include Stdlib H Starter Code To Finish Const Unsigned Uns Max 1 1 (22.94 KiB) Viewed 37 times
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
//starter code to finish:
const unsigned UNS_MAX = -1; //
1111...
const unsigned UNS_MIN = 0; //
0000...
const int INT_MAX = UNS_MAX >> 1; // 0111...
const int INT_MIN = ~INT_MAX;
typedef struct {
char** cars;
size_t* len;
size_t* alloc_size;
} stringbuilder;
stringbuilder new_sb() {
stringbuilder sb;
sb.cars = malloc(sizeof(char*));
*sb.cars = malloc(8*sizeof(char));
(*sb.cars)[0] = 0;
sb.len = malloc(sizeof(size_t));
*sb.len = 0;
sb.alloc_size = malloc(sizeof(size_t));
*sb.alloc_size = 8;
return sb;
}
void sb_append(stringbuilder sb, char a) {
int len = *sb.len;
if (len >= (*sb.alloc_size)-1) {
*sb.alloc_size = (*sb.alloc_size)*2;
char* newcars =
malloc((*sb.alloc_size)*sizeof(char));
for (int i = 0; i < *sb.len; i++) {
newcars = (*sb.cars);
}
free(*sb.cars);
(*sb.cars) = newcars;
}
(*sb.cars)[len] = a;
len++;
(*sb.cars)[len] = 0;
*sb.len = len;
}
void delete_sb(stringbuilder sb) {
free(*sb.cars);
free(sb.cars);
free(sb.len);
free(sb.alloc_size);
}
bool sb_is_equal(stringbuilder sb1, stringbuilder sb2) {
if (*sb1.len != *sb2.len)
return false;
for (int i = 0; i < *sb1.len; i++) {
if ((*sb1.cars) != (*sb2.cars))
return false;
}
return true;
}
void print_sb(const stringbuilder sb) {
printf("%s", *sb.cars);
}
// Task 4
stringbuilder task_4_ans =
task_4_check(x);
stringbuilder task_4 = get_bin_2(x);
printf("\n");
if (!sb_is_equal(task_4, task_4_ans))
printf("FAILED");
else
printf("PASSED");
printf(": GET BINARY 2 (Task 4)\n");
printf("Expected: \"%s\"\n",
*task_4_ans.cars);
printf(" Got: \"%s\"\n",
*task_4.cars);
delete_sb(task_4_ans);
delete_sb(task_4);
printf("\n");
printf("Input a number (1234 to exit): ");
scanf("%d", &x);
Task 4 Get Binary 2: Implement the get_bin_2 function as directed in the comment above the function. If the directions are unclear, run the code, and look at the expected answer for a few inputs. For your solution, you should refer to your solution for Task 2 to get the general idea for Task 4, and you should use largest_po2_le in your solution (think about why this might be useful in terms of the binary representation). Make sure you build the string for 0 and negative numbers correctly. The same restrictions apply from Task 2.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply