Skip to content

Commit

Permalink
Submission
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat committed Feb 6, 2020
1 parent a835bec commit 4b06a15
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
57 changes: 57 additions & 0 deletions exercises/ex02/adder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Exercise 2
* Maalvika Bhat
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int processInput(char input[]) {
int val = atoi(input);

if (val == 0) {
puts("input not valid");
} else if (strlen(input) > 10) {
puts("input is too large");
} else {
printf("input of %i has been added\n", val);
return val;
}
return 0;
}

void addIntegers(int integers[]) {
int sum = 0;
for (int i = 0; i < 10; ++i) {
sum += integers[i];
}
printf("final sum of all the integers is %i\n", sum);
}

void run() {
char input[5];
int integers[10];
int count = 0;

puts("enter up to 10 integers to add");
while (scanf("%10s", input) != EOF) {
if (count < 10) {
int result = processInput(input);

if (result != 0) {
integers[count] = result;
count++;
}
} else {
puts("press CTRL+D to add the integers up");
continue;
}
}
addIntegers(integers);
}

int main() {
run();
return 0;
}
23 changes: 16 additions & 7 deletions exercises/ex02/aspace.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/* Example code for Think OS.
Copyright 2014 Allen Downey
License: GNU GPLv3
*/

#include <stdio.h>
#include <stdlib.h>

int var1;

void printLocalVariable() {
int a = 0;
printf("Address of local variable a is %p\n", &a);
}

int main ()
{
int var2 = 5;
Expand All @@ -22,5 +20,16 @@ int main ()
printf ("p points to %p\n", p);
printf ("s points to %p\n", s);

p = malloc(10);
printf ("Address of p is now %p\n\n", p);

printLocalVariable();

void *b = malloc(10);
void *c = malloc(10);

printf ("Address of b is %p\n", b);
printf ("Address of c is %p\n\n", c);

return 0;
}
18 changes: 8 additions & 10 deletions exercises/ex02/stack.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
/* Example code for Think OS.
Copyright 2014 Allen Downey
License: GNU GPLv3
*/

#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

int *foo() {
int i;
int array[SIZE];
int array[SIZE]; // local array

printf("%p\n", array);

Expand All @@ -24,7 +17,7 @@ int *foo() {

void bar() {
int i;
int array[SIZE];
int array[SIZE]; // local array

printf("%p\n", array);

Expand All @@ -36,7 +29,7 @@ void bar() {
int main()
{
int i;
int *array = foo();
int *array = foo(); // assigning a pointer to a function's local array
bar();

for (i=0; i<SIZE; i++) {
Expand All @@ -45,3 +38,8 @@ int main()

return 0;
}

/* The arrays initialized in foo() and bar() are located at the same address.
* Both arrays were local variables that were only stored in the stack while
* their respective functions were running.
*/

0 comments on commit 4b06a15

Please sign in to comment.