Tracing through a program :D
int maximum(int fdata[], int n)
{
int i, fmax;
fmax = fdata[0];
for (i=0; i
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.
okay so what are you trying to do here, just get a max value out of the 100 random number list
Well I'm trying to trace through the program step by step to see the outcome.
okay so u want to add stuff in that displays address spaces in here now?
We start in int main function where we have integer data invoking an array of 100 numbers, integer i and integer max.
No, we just explain how the program begins and starts going through every step until it reaches the outcome.
oh okay
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
-generate 100 random number and stored in list -feed the list into the other max finding function -and returns the max
i wonder if you can avoid passing in the 100 though, isnt htere some length of array function in C
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; }
what do you mean passing in the 100?
this line max = maximum(data,100); <---
oh that is just calling out the function. I don't know how it would affect it.
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); }
oh nvm we do need the 100
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; }
i guess it shud say int k
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); }
yeah theres no need for that mat
oh to be honest they haven't taught me to put anything inside the int main() parenthesis other than void
so I just know the long way
okay wait
lemme see if i got a c compiler somewhere
okay so is this just "C" or "C++"
C
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.
hey try this code out see if its working
#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; }
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
what do u mean we are putting in a value into fdata
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'
the compiler can't run it.
oh I want to know from where fdata comes from.
since is not called out in neither of the functions.
fadata is just the name of a variable
you are inputing it in max = maximum(data,100);
fdata and data share the same address space
don't you need to create a variable first before you can call it?
oh ok so if I wanted I could of put tacos instead of fdata
u can do that
alright
and btw your program game an error on the main function
ya i dunno its working fine on this compiler
http://www.tutorialspoint.com/compile_c_online.php you can test out code there too
i dont have eclipse and that stuff so
ooo
I don't recall being able to use sizeof()
that makes things easier although both are the same thing so np
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; }
fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; what is really happening there?
fmax = fdata[0] which fdata is 1 number. so fmax = 0 right?
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. ??
so if ( array of 100 random numbers from data is greater than 0?
fdata[i] gives u the ith indexed value in that list
it checks every place in the list
so it checks every single random number created to see if its greater than fmax
|dw:1430990261640:dw|
Join our real-time social learning platform and learn together with your friends!