enable inputs
This commit is contained in:
+18
-24
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -10,17 +9,16 @@ struct IntArrayStr{
|
|||||||
};
|
};
|
||||||
|
|
||||||
void read_int_array_str(struct IntArrayStr *in_str) {
|
void read_int_array_str(struct IntArrayStr *in_str) {
|
||||||
printf("inputlength:%zu\n", in_str->length);
|
|
||||||
for (size_t i = 0; i < in_str->length; i++) {
|
for (size_t i = 0; i < in_str->length; i++) {
|
||||||
printf("digit:%zu = %d\n", i, in_str->data[i]);
|
printf("digit:%zu = %d\n", i, in_str->data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IntArrayStr *create_int_array_str(size_t in_capacity) {
|
struct IntArrayStr *create_int_array_str(size_t in_capacity) {
|
||||||
struct IntArrayStr *mystr = malloc(sizeof(struct IntArrayStr)); /* allocate memory for struct */
|
struct IntArrayStr *mystr = malloc(sizeof(struct IntArrayStr)); //allocate memory for struct
|
||||||
|
|
||||||
mystr->data = malloc(sizeof(int) * in_capacity); /* make sure the pointer in the struct points to the array */
|
mystr->data = malloc(sizeof(int) * in_capacity); //make sure the pointer in the struct points to the array
|
||||||
mystr->length = 0; //forgot to actually assign a value to the struct
|
mystr->length = 0; //forgot to actually assign a value to the struct
|
||||||
mystr->capacity = in_capacity;
|
mystr->capacity = in_capacity;
|
||||||
|
|
||||||
if (mystr->data == NULL) {
|
if (mystr->data == NULL) {
|
||||||
@@ -30,39 +28,35 @@ struct IntArrayStr *create_int_array_str(size_t in_capacity) {
|
|||||||
return mystr;
|
return mystr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* push value to struct, also handle realloc */
|
||||||
void push_int_array_str(int in_int, struct IntArrayStr *in_str){
|
void push_int_array_str(int in_int, struct IntArrayStr *in_str){
|
||||||
if (in_str->length == in_str->capacity) {
|
if (in_str->length == in_str->capacity) { //check if already full
|
||||||
in_str->capacity*=2;
|
in_str->capacity*=2; //if so we should double
|
||||||
printf("intarray capacity is now:%zu\n", in_str->capacity);
|
printf("intarray capacity is now:%zu\n", in_str->capacity);
|
||||||
int *temp;
|
int *temp; //create a temporary pointer
|
||||||
temp = realloc(in_str->data, sizeof(int)*in_str->capacity);
|
temp = realloc(in_str->data, sizeof(int)*in_str->capacity); //realloc the data into a new bucket
|
||||||
if(temp == NULL){exit(0);};
|
if(temp == NULL){exit(0);}; //check if actually valid
|
||||||
in_str->data = temp;
|
in_str->data = temp; //if valid, make sure the pointer in the struct points to the realloced block
|
||||||
printf("intarray length is now:%zu\n", in_str->length);
|
|
||||||
}
|
}
|
||||||
in_str->data[in_str->length] = in_int;
|
in_str->data[in_str->length] = in_int;
|
||||||
printf("number:%zu = %d\n", in_str->length, in_str->data[in_str->length]);
|
printf("number:%zu = %d\n", in_str->length, in_str->data[in_str->length]);
|
||||||
in_str->length++; //didn't read the flow when transfering and accidentally put this in the if block
|
in_str->length++; //didn't read the flow when transferring and accidentally put this in the if block
|
||||||
|
printf("intarray length is now:%zu\n", in_str->length);
|
||||||
//also had an off-by-one error because I increased length before doing anything
|
//also had an off-by-one error because I increased length before doing anything
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
// change to while loop and take inputs rather than fill with random numbers
|
|
||||||
printf("input numbers:\n");
|
printf("input numbers:\n");
|
||||||
|
|
||||||
//int input;
|
|
||||||
size_t length = 10;
|
|
||||||
/* while (input != -1) { */
|
|
||||||
/* scanf("%d", &input); */
|
|
||||||
/* length++; */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
struct IntArrayStr *myintarraystr = create_int_array_str(2);
|
struct IntArrayStr *myintarraystr = create_int_array_str(2);
|
||||||
|
|
||||||
for (size_t i = 0; i < length; i++) {
|
int input;
|
||||||
int input = rand() % 10;
|
size_t length = 10;
|
||||||
push_int_array_str(input, myintarraystr);
|
|
||||||
|
while (input != -1) {
|
||||||
|
scanf("%d", &input);
|
||||||
|
push_int_array_str(input, myintarraystr);
|
||||||
}
|
}
|
||||||
|
|
||||||
read_int_array_str(myintarraystr);
|
read_int_array_str(myintarraystr);
|
||||||
|
|||||||
Reference in New Issue
Block a user