array parts

This is a discussion on array parts within the C Programming forums, part of the General Programming Boards category; I i am just studying modulization in C. Now im a bit confused. What i want to do is the ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    array parts

    I i am just studying modulization in C. Now im a bit confused.
    What i want to do is the user inputs 10 numbers and they are stored in an array. Then the numbers are each passed to a module (procedure) to determine if its the maximum value. How would i go about the modules?
    Below i created a brief try, ignoring the like syntax errors, is this how i could pass the array to another module etc?
    Thanks

    Code:
    int main(int argc, char ** argv)
    {
    int i;
    float array[i];
    	for(i=0;i<10;i++)
    	{
    		fprintf(output,"Please enter your values");
    		fscanf(input,"%f",&array[i]);
    		maximum_num(array[i]);
    	}
    	fprintf(output,"Maximum number is %f\n",&maximum_num);
    }
    
    int maximum_num(float array_size);
    {
    float current_max = 0;
    if(array[i]>current_max)
    	return current_max = array[i];
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    generally, you do something like this:
    Code:
    int maxnum(int *arr, int size);  // prototype. 
    int main()
    {
        int array[10];  // In C the size of arrays is always a numeric constant, not a variable. 
        int max;
        ...
        max = maxnum(arr, 10);
    }
    
    int maxnum(int *arr, int size)
    {
       ... 
    }
    You should probably use a #define for the number 10 so that you don't have to repeat it in different places.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    ok i get that but still a bit confused, i tried this now:

    Code:
    #include <stdio.h>
    FILE * output =NULL;
    FILE * input =NULL;
    static int max(float array[i])
    {
    
        float current_max = 0.0;
        if(array[i]>current_max)
    	current_max=array[i]  
    
    }
    static int display(float array[i])
    {
        for(i=1; i<10; i++)
        	fprintf(output,"%f ",array[i]);    
        return();
    }
    
    
    int main(int argc, char * argv[])
    {
    float array[i];
    
        output = stdout;
        input = stdin;
    
        for(i=0;i<10;i++)
        fprintf(output,"Please enter value: ");
        fscanf(input,"%f",&array[i]);
    
    }
    Now i am a bit confused how i can take a value from the array of values and the pass it to one fuction to determine if its the max value, then continue that until they have all been down, finally pass the numbers to another procedure to display all the numbers??

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't use i as a "size" for arrays, as I explained in the previous post. For passing arrays, you either use "float *array" or "float array[]" - in either case, you need some way to determine that the array ends. Passing the number of elements as a parameter to your function is one way. Another is to mark the end with a special marker, such as -1 (assuming for example valid numbers are all positive).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    otherwise it should work?

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    so how do i pass an array of elements to another module to then perform a task?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void blah(type *arr);
    ... 
    int main()
    {
        type array[10];
    ....
        blah(array);
    ...
    }
    
    void blah(type *arr) 
    {
       ...  ... arr[x] ... ... 
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    alright i fixed it up, jus one little problem that i have no idea:

    Code:
    #include <stdio.h>
    FILE * output =NULL;
    FILE * input =NULL;
    
    void display(float array[10])
    {
    int i;
    	for(i=0; i<10; i++)
    	{
    		fprintf(output,"%.4f ",array[i]);
    		fprintf(output,"\n");
    	}
    }
    
    float max(float array[10])
    {
    int i;
    	float max=0.0;
    	//for(i=0; i<10; i++)
    	//{
    	if(array[i]>max)
    	{
    	max=array[i];
    	}
    	//}
    	fprintf(output,"The maximum value is %f",&max);
    
    }
    
    int main(int argc, char * argv[])
    {
    int i;
    float n;
    float array[10];
    
        output = stdout;
        input = stdin;
    	for(i=0; i<10; i++)
    	{
        	fprintf(output,"Enter value ");
        	fscanf(input,"%f",&array[i]);
    	}
    	display(array);
    	//max(array);
    	fprintf(output,"%f\n",max(array));
    
    
    }
    Now the program is supposed to pass the array to the max module to find the biggest number, but doesnt work. if i put:

    Code:
    fprintf(output,"%f\n",max(array));
    in the for loop in the main module it works, but once i put it outside like how it looks now it doesnt work, im not sure how to fix that?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What compiler are you using? Don't you have warnings enabled on it?

    Your compiler should tell you that you are not returning a value from your max function (and you also have used the same name for a function and a variable, whilst this is valid as such, it's a bad thing to do, because it's very confusing to anyone reading the code).

    Code:
    fprintf(output,"The maximum value is %f",&max);
    I bet this doesn't print the right value at all!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    could you help me out with that, like how would i give the array to the max function and then find the max num?

    (the code above might not work correctly because i was playing around and cant compile it because i dont have linux)

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "I haven't got Linux"? None of the code in your example is relying on Linux, as far as I can see.

    And yes, I could fix the code up, but I don't see why I should do that - I have given you enough suggestions that you should be able to do it yourself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    float max(float * arr, size_t n)
    {
        size_t i = 0;
        float m = 0.0f;
    
        /* maybe some loop here? */
    
        return m;
    }
    Something like that, you're close - just listen to what matsp says!
    Guessing is no way to get something done, I also don't see what Linux has to do with anything

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    well i use linux to compile it

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Quote Originally Posted by zacs7 View Post
    Code:
    float max(float * arr, size_t n)
    {
        size_t i = 0;
        float m = 0.0f;
    
        /* maybe some loop here? */
    
        return m;
    }
    Something like that, you're close - just listen to what matsp says!
    Guessing is no way to get something done, I also don't see what Linux has to do with anything
    what do u mean by the float m = 0.0f??

    and

    do i need the * arr etc? because i used it somewhere else without it and it works?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can "receive" an array in several different ways in C:
    Code:
    func1(int *arr);
    func2(int arr[]);
    func3(int arr[10]);
    All of which are essentially identical - the last one enforces the size of the array passed, so you can't for example pass a 20 element array to that function. The first two take "any size array".

    This is also why zacs7 suggested you pass in a size_t n to indicate the size of the array. That way, your function works for all sorts of number of values, rather than ALWAYS having to have 10 values in the array.

    m = 0.0f means set the value m to zero-point-zero, and it's a floating point number (rather than a double precision that 0.0 would be).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Inserting new member into array of structs
    By cathym in forum C Programming
    Replies: 6
    Last Post: 04-17-2005, 07:10 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21