Help

This is a discussion on Help within the C Programming forums, part of the General Programming Boards category; here is my big probelm, it is going to first include the initial programming instructions and part of my program ...

  1. #1
    cwd
    cwd is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Help

    here is my big probelm, it is going to first include the initial programming instructions and part of my program to try to solve it, I'm stuck big time.

    Write a C program incorporating a main() function and at least 2 other functions which will do the following:
    Create an array of 15 doubles and initialize it as follows:
    the 0 through 4 elements : 14.83,19.24,0.84,1.98,3.47
    and the 10-14 elements : 827.39,632.85,471.38,0,04,4.83
    and all other elements 0.
    Consider the two groups of non-zero numbers in the array as lists and create four pointers such that there is a pointer to the beginning(element 0) of the first list and the end(element 4) of the first list and likewise for the second list. Use names like first and last or hedad and tail or front and back to name your pointers.
    ***Call a function to print out the two lists,one list per line.
    ***Call a function to print out the values in the 4 first and last list elements with the names of the pointer variables that point to them.
    Using array notation,sum the elements in the first list(0-4) and sum the elements in the second list(10-14) and subtract the sum of the first list from the sum of the second list. ***Print out the difference in a readable sentence.
    ***Call the print fucntion from above to print both lists again.
    Call an insert fucntion to insert the value 1298.8314 at the end of the first list using pointer notation. Make sure the list pointers are updated.
    ***Call the print function from above to print both lists again.
    ***Call a fucntion to print out the values in the 4 first and last list elements with the names of the pointer variables that point to them.
    Call an insert function to insert the value 0.369 as the new second element of the second list using pointer notation. Make sure the list pointer are updated.
    ***Call the print function from above to print both lists again.
    ***Call a function to print out the values in the 4 first and last list elements with the names of the pointer variables that point to them.
    Using array or pointer notation, sum the elements in the current first list and sum the elements in the current second list and subtract the sum of the first list from the sum of the second list.
    ***Print out the difference in a readable sentence.
    ***Call the print function from above to print both lists again.

    Here is my program so far and I'm stuck.

    #include <stdio.h>


    void arrayprint(double numArray[15]);
    void pointerprint(double numArray[15],double *first,double *last,double *front,double *back);
    void arraysubtract(double numArray[15]);
    void arrayinsert(double numArray[15]);

    int main()
    {
    double numArray[15]={14.83, 19.24, 0.84, 1.98, 3.47, 0, 0, 0, 0, 0, 827.39,
    632.85, 471.38, 0.04, 4.83};
    double *first=numArray;
    double *last=first+4;
    double *front=first+10;
    double *back=first+14;
    arrayprint(numArray);
    pointerprint(numArray,first,last,front,back);
    arraysubtract(numArray);
    arrayinsert(numArray);
    return 0;
    }

    void arrayprint(double numArray[15])
    {
    int i;
    for(i=0; i<15; i++)
    {
    if(i<5)
    {
    printf(" %1.2f", numArray[i]);
    }
    else if(i==5)
    {
    printf("\n");
    }
    else if(i>9)
    {
    printf(" %1.2f", numArray[i]);
    }
    }
    }

    void pointerprint(double numArray[15],double *first,double *last,double *front,double *back)
    {
    printf("\n");
    printf("first %1.2f last %1.2f front %1.2f back %1.2f\n",*first,*last,*front,*back);

    }

    void arraysubtract(double numArray[15])
    {



    }

    void arrayinsert(double numArray[15])
    {

    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > ***Call a function to print out the two lists,one list per line.
    I think this should be
    void arrayprint(double *start, double *end );

    And you would call it twice, like so
    arrayprint(first,last);
    arrayprint(front,back);

    Once you have set up your four pointers (which you have done correctly), the rest of the code should just be using first, last, front, back.

    None of your functions should have double numArray[15] in them, just pairs of double* variables pointing to the ends of the list of doubles.

    > void pointerprint
    To print a pointer, and the double it points to, do this (say start is the name of the pointer parameter)
    printf( "start=%p, val=%f\n", start, *start );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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