Ask your own question, for FREE!
Computer Science 21 Online
OpenStudy (anonymous):

Tracing through a program :D int maximum(int fdata[], int n) { int i, fmax; fmax = fdata[0]; for (i=0; i fmax) fmax = fdata[i]; return(fmax); } int main() { int data[100], i, max; for (i=0; i<100; i++) data[i] = rand() % 100; max = maximum(data,100); printf("Max = %d\n", max); return 0; }

OpenStudy (anonymous):

So first we assume that we have #include<stdio.h> and so forth otherwise this program woudn't even compile because the computer wouldn't be able to detect some of the syntaxes that are included in stdio.h being used in this program.

OpenStudy (dan815):

okay so what are you trying to do here, just get a max value out of the 100 random number list

OpenStudy (anonymous):

Well I'm trying to trace through the program step by step to see the outcome.

OpenStudy (dan815):

okay so u want to add stuff in that displays address spaces in here now?

OpenStudy (anonymous):

We start in int main function where we have integer data invoking an array of 100 numbers, integer i and integer max.

OpenStudy (anonymous):

No, we just explain how the program begins and starts going through every step until it reaches the outcome.

OpenStudy (dan815):

oh okay

OpenStudy (anonymous):

int main() { int data[100], i, max; for (i=0; i<100; i++) data[i] = rand() % 100; max = maximum(data,100); printf("Max = %d\n", max); return 0; } we start here

OpenStudy (dan815):

-generate 100 random number and stored in list -feed the list into the other max finding function -and returns the max

OpenStudy (dan815):

i wonder if you can avoid passing in the 100 though, isnt htere some length of array function in C

OpenStudy (anonymous):

int maximum(int fdata[], int n) { int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; return(fmax); } int main() { int data[100], i, max; for (i=0; i<100; i++) data[i] = rand() % 100; max = maximum(data,100); printf("Max = %d\n", max); return 0; }

OpenStudy (anonymous):

what do you mean passing in the 100?

OpenStudy (dan815):

this line max = maximum(data,100); <---

OpenStudy (anonymous):

oh that is just calling out the function. I don't know how it would affect it.

OpenStudy (dan815):

int maximum(int fdata[]) <---- get rid of the extra parameter int n n=length(fdata[]) { int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; return(fmax); }

OpenStudy (anonymous):

oh nvm we do need the 100

OpenStudy (dan815):

int main(k) <--- now u can work with varying list sizes { int data[k], i, max; for (i=0; i<k; i++) data[i] = rand() % 100; max = maximum(data); printf("Max = %d\n", max); return 0; }

OpenStudy (dan815):

i guess it shud say int k

OpenStudy (anonymous):

int maximum(int fdata[], int n) <--- int n becomes 100 on { int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) <--- here, n becomes 100 if (fdata[i] > fmax) fmax = fdata[i]; return(fmax); }

OpenStudy (dan815):

yeah theres no need for that mat

OpenStudy (anonymous):

oh to be honest they haven't taught me to put anything inside the int main() parenthesis other than void

OpenStudy (anonymous):

so I just know the long way

OpenStudy (dan815):

okay wait

OpenStudy (dan815):

lemme see if i got a c compiler somewhere

OpenStudy (dan815):

okay so is this just "C" or "C++"

OpenStudy (anonymous):

C

OpenStudy (anonymous):

I'm confused on the maximum function int maximum(int fdata[], int n) { int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; return(fmax); } how is fdata working without a value? (I mean as calling it int fdata[0] in the second function since on the first it doesn't exist.

OpenStudy (dan815):

hey try this code out see if its working

OpenStudy (dan815):

#include <stdio.h> int maximum(int fdata[]) { int n =sizeof(fdata); int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; return(fmax); } int main() { int k=100; int data[k], i, max; for (i=0; i<k; i++) data[i] = rand() % 100; max = maximum(data); printf("Max = %d\n", max); return 0; }

OpenStudy (anonymous):

I know max = maximum(data,100); sends data(the 100 random numbers), and 100 for n. So on int maximum (int fdata[], int n) <-- data becomes fdata[] and int n becomes 100. so in fmax = fdata[0] <--- meaning that there is an array of 1 number. . im lost

OpenStudy (dan815):

what do u mean we are putting in a value into fdata

OpenStudy (anonymous):

practic.c:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token practic.c: In function 'main': practic.c:21: warning: incompatible implicit declaration of built-in function 'printf'

OpenStudy (anonymous):

the compiler can't run it.

OpenStudy (anonymous):

oh I want to know from where fdata comes from.

OpenStudy (anonymous):

since is not called out in neither of the functions.

OpenStudy (dan815):

fadata is just the name of a variable

OpenStudy (dan815):

you are inputing it in max = maximum(data,100);

OpenStudy (dan815):

fdata and data share the same address space

OpenStudy (anonymous):

don't you need to create a variable first before you can call it?

OpenStudy (anonymous):

oh ok so if I wanted I could of put tacos instead of fdata

OpenStudy (dan815):

u can do that

OpenStudy (anonymous):

alright

OpenStudy (anonymous):

and btw your program game an error on the main function

OpenStudy (dan815):

ya i dunno its working fine on this compiler

OpenStudy (dan815):

http://www.tutorialspoint.com/compile_c_online.php you can test out code there too

OpenStudy (dan815):

i dont have eclipse and that stuff so

OpenStudy (anonymous):

ooo

OpenStudy (anonymous):

I don't recall being able to use sizeof()

OpenStudy (anonymous):

that makes things easier although both are the same thing so np

OpenStudy (anonymous):

int maximum(int fdata[], int n) { int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; return(fmax); } int main() { int data[100], i, max; for (i=0; i<100; i++) data[i] = rand() % 100; max = maximum(data,100); printf("Max = %d\n", max); return 0; }

OpenStudy (anonymous):

fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; what is really happening there?

OpenStudy (anonymous):

fmax = fdata[0] which fdata is 1 number. so fmax = 0 right?

OpenStudy (anonymous):

for (i=0; i<n; i++) if (fdata[i] > fmax) then a loop of 100 is created and an if statement happens where (fdata[i] <-- i being 100 so we have an array of 100... > fmax <-- fmax being = 0. ??

OpenStudy (anonymous):

so if ( array of 100 random numbers from data is greater than 0?

OpenStudy (dan815):

fdata[i] gives u the ith indexed value in that list

OpenStudy (dan815):

it checks every place in the list

OpenStudy (anonymous):

so it checks every single random number created to see if its greater than fmax

OpenStudy (dan815):

|dw:1430990261640:dw|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!