needs help copying arrays

This is a discussion on needs help copying arrays within the C Programming forums, part of the General Programming Boards category; I need to make a program that copies arrays among functions using both pointer notation and array notaion. I can't ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Array help needed, again...

    I need to make a program that copies arrays among functions using both pointer notation and array notaion. I can't see mto get it to work to pass the array on to the next. Any help would be greatly appreciated.


    Code:
    #include <stdio.h>
    #define SIZE 6
    
    double main_array(void); 
    double array_notation(double ar[], int);
    double pointer_notation(double ar[], int);
    double largest_value(double ar[], int);
    double display(double ar[], double ar[], double ar[], double);
    
    int main(void)
    {
    double array1, array2, array3, largest, layout;
    
    array1 = main_array();
    array2 = array_notation(array1, SIZE);
    array3 = pointer_notation(array1, SIZE);
    largest = largest_value(array1, SIZE);
    layout = display(array1, array2, array3, largest);
    return 0;
     }
    
    double main_array(void) 
    {
    double array1[SIZE] = {33.0,13.0,11.3,108.2,3.1,2.6};
    int index;
    
    for (index = 0; index < SIZE; index++)
    	printf("Number %d has %lf for a value.\n", index +1, array1[index]);
    }
     
    double array_notation(double array1[], int n)
    {
    double array2[SIZE];
    int index;
    
    array2[SIZE] = array1[SIZE];
    for (index = 0; index < SIZE; index++)
    	printf("Number %d has %lf for a value.\n", index +1, array2[index]);
    	printf("2\n\n");
    }
    
    double pointer_notation(double array1[], int n)
    {
    int i;
    double array3[SIZE];
    int index; 
    
    array3[SIZE] = array1[SIZE];
    for (index = 0; index < SIZE; index++)
    	printf("Number %d has %lf for a value.\n", index +1, *(array3 + index));              
    	printf("3\n\n");
    }
    
    double largest_value(double array1[], int)
    {
    
    double greatest = 0.0;
    double temp = 0.0;
    int number = 0;
    
    for (number = 0; number < SIZE; number++)
    	do
    	{
    		if (temp >= greatest && greatest == 0.0)
    		{
    			greatest = temp;
    		}
    	else 
    		if (temp > greatest && greatest != 0.0) 
    		{
    			greatest = temp;
    		}
    	} while (number != SIZE);
    return greatest;
    }
    
    double display(double array1[], double array2[], double array3[],double largest)
    {
    int i;
    
    printf ("%2s%10s%10s%10s\n", "i", "array1", "array2", "array3");
    for (i = 0; i < SIZE; i++)
    	printf ("%2d%10d%10d%10d\n", i, array1[i], array2[i], array3[i]);
    printf("The largest value stored in the array is %lf.\n", largest);
    return 0;
    }
    Last edited by RedRattler; 04-04-2003 at 10:25 PM.

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    From just reading through quickly, i noticed this first
    Code:
    double array1, array2...... //and so forth
    are not declaring arrays, they are just creating variables of type double,you then try to pass them as arrays, it will not work, second i noticed this:
    Code:
    array2[SIZE] = array1[SIZE];
    When you do this and #define SIZE 6. It will try to refrence array1[6] which is not part of the array, remember in C, arrays go from 0 - (SIZE-1). You shouldn't try to reference an array element using that define. Also, if you want the whole array to = the other array, you need to loop through, element by element to copy them.
    Last edited by stumon; 04-04-2003 at 10:25 PM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    thanks Stumon.

    I will play with that and see if I can get it.

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Also, you have gotten a lot of your function calls different then your prototypes with what is passed and returned. You can not return something into an array. It seems you want to pass something as an array to all the functions, the return something back into that array, it wont work like that, you will have to pass the array name into a pointer, then change what ever needs to be changed and then there is no need for returning anything, it is already changed, so all you do it pass the array name again and use its values, they will already be changed, and if you wanted to change them again, pass a pointer to that array again, and so forth. That is the main problem, you are passing arrays and trying to return arrays, and so forth. Get everything striaght with what you pass, return, and need to edit.

    --EDIT--
    LOL, i got bored, so here is your code, all in working order. I havent commented it becuase im tired, sorry, if you have any questions, just reply.
    Code:
    #include <stdio.h>
    #define SIZE 6
    
    void main_array(double []); 
    void array_notation(double[]);
    void pointer_notation(double[]);
    double largest_value(double[]);
    void display(double[], double);
    
    int main(void)
    {
    double largest;
    double array1[SIZE] = {0};
    
    main_array(array1);
    array_notation(array1);
    pointer_notation(array1);
    largest = largest_value(array1);
    display(array1, largest);
    return 0;
     }
    
    void main_array(double array1[]) 
    {
    int index;
    
    for (index = 0; index < SIZE; index++)
    	 array1[index] = ((double)index*23.4);
    
    for (index = 0; index < SIZE; index++)
    	printf("Number %d has %lf for a value.\n", index +1, array1[index]);
    }
    
    void array_notation(double array1[])
    {
    double array2[SIZE];
    int index, i;
    
    for (i = 0; i < SIZE; i++)
    	array2[i] = array1[i];
    
    for (index = 0; index < SIZE; index++)
    	printf("Number %d has %lf for a value.\n", index +1, array2[index]);
    	printf("2\n\n");
    }
    
    void pointer_notation(double array1[])
    {
    double array3[SIZE];
    int index, i; 
    
    for (i = 0; i < SIZE; i++)
    	array3[i] = array1[i];
    
    for (index = 0; index < SIZE; index++)
    	printf("Number %d has %lf for a value.\n", index +1, *(array3 + index));              
    	printf("3\n\n");
    }
    
    double largest_value(double array1[])
    {
    
    double greatest = 0.0;
    int number = 0;
    
    for (number = 0; number < SIZE; number++)
    	if (array1[number] >= greatest && greatest == 0.0)
    		{
    			greatest = array1[number];
    	}
    	else 
    	if (array1[number] > greatest && greatest != 0.0) 
    		{
    			greatest = array1[number];
    	}
    return greatest;
    }
    
    void display(double array1[], double largest)
    {
    int i;
    
    printf ("%2c%10s\n", 'i', "array1");
    for (i = 0; i < SIZE; i++)
    	printf ("%2d%10.3f\n", (i + 1), array1[i]);
    printf("The largest value stored in the array is %.2lf.\n", largest);
    
    }
    Last edited by stumon; 04-04-2003 at 11:19 PM.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Is this poor coding? copying arrays of doubles
    By Hansie in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 02:34 PM
  3. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 01:08 PM
  4. Copying Arrays
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 03:09 PM
  5. copying character arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 05:39 PM

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