Thread: I need help writing a c++ program please..

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    I need help writing a c++ program please..

    ok im in a computer programming class, I really do not understand it to well. Ive gotten threw the first 4 projects but i cant seem to pass the next 1. I am gonna write what my paper says. and if some1 would be able to help me out with some code or whatever they can possible help me with I would greatly appreciate it.

    I realize that it seems like a lot, but the thing is i honestly dont kno how to do this, and the teacher isnt helpin me out. I know that people who know about C++ can do this program in about a minute. so please help me out. thank you

    1. Declare a single dimensioned array of type integer that can have up to 50 elements.

    2. Input values, one at a time into the array from the data file, until there are no more values to read, keeping a count as you go.

    3. Sort the Array into ascending order utilizing a void function.

    4. the sort function is to utilize another void function to swap 2 values.

    5. Output is to print the original data and the sorted data side-by-side.

    6. calculate the average found in the array using a value-returning function. Entire array must be passed to function which will add number of values indicated by counter 'n'.

    7. display a count of the number of values read in, the largest value, and the smallest value in the array.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. Lookup these C++ tools.

    cin
    cout
    for-loop

    Here is the array.

    int array[50];

    Kuphryn

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I am sorry that you are having trouble. But it would just not be right for me to give you the answers. I'll give you direction though.

    You can sort using the qsort() function found in cstdlib

    Code:
    #include <cstdlib>
    You can declare an array of ints the same way you declare an array of chars

    Code:
    int my_array[50];
    To use qsort() must write a function to declare two entries.

    Code:
    int my_compare(const int *param1, const int *param2) {
        return *param1 - *param2;
    }
    The rest I'll leave up to you. I should point out that you must use type casting in order to use my sample compare function.

    Code:
    qsort(some_array, array_size, sizeof(int), (int(*)(const void*, const void *))my_compare);
    Enjoy.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Just take the problems one by one.

    1. Declare a single dimensioned array of type integer that can have up to 50 elements.

    Do you know how to declare normal variable like:
    int x;
    double y;
    If you do declaring arrays shouldn't be a big problem, it's done like this:
    DataType NameOfArray[NumberOfElements]
    for example:
    int x[10];
    is it clear now...
    And I think if you are using a C++ book it should be there just go and check it out.
    ***********************************************
    2. Input values, one at a time into the array from the data file, until there are no more values to read, keeping a count as you go.

    first you should know how to access each element in the array and it's like this:
    if you have a five-element-array int array[5];

    The elements will be:
    array[0]
    array[1]
    array[2]
    array[3]
    array[4]
    So you access any of them.
    In order to input values one at a time, you have to make a loop that repleated n times where n is the number of elements and each time input one element...
    I'll not write the code for you, you should try, if you couldn't I will give you more help.

    So try to solve thte first two part's and them we can deal with the rest, but you should work on it!

    Post you work and then we can continue.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    ok so for the 1st part...the syntax would be like first
    the
    #include <iostream.h>


    void main()
    int x[50];

    And for the 2nd part it would look somethin like this? im not to sure i really suck at this!!

    indata.open ("computer.dat");
    indata >>count;

    while (count>=0 && count<=50)
    {
    indata >>number;

    cout<<"value="<<x<<endl;

    count ++

    }

  6. #6
    MarkMX
    Guest

    AHH

    Void Main!

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Originally posted by Salem
    carrman, apart from the actual sorting, you should be able to have a go at the rest of it


    Code:
    int my_compare(const int *param1, const int *param2) {
        return *param1 - *param2;
    }
    Can you spot the bug?
    Like I said I'm not going to do everything for this person. I am also aware that using qsort would be illegal for this assignment since it asks for the student to write the sort algorithm.

    Other than that I do not see anything wrong with that function...

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    How dare you insult my function Although you are correct I was under the impression that this would work fine for this usage. But Salem is correct...This will only works with 100% certainty if the number is of short size.

  9. #9
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    read the comments
    ok so for the 1st part...the syntax would be like first
    the
    #include <iostream.h>


    void main()
    int x[50]; //this is a global variable is that what yo need?

    And for the 2nd part it would look somethin like this? im not to sure i really suck at this!!

    //you should declare indata first
    indata.open ("computer.dat"); //So you want to read from a file?
    indata >>count;

    /*the loop is correct but also you have to declare and initialize count, and also you can use this:
    for ( int i=0; i<50; i++)
    {
    indata>>number;
    cout << number;
    }
    also you have to declare number.

    but I think the correct way to so that is, since you've declares an array - int x[50]; - just store the values in it.
    for ( int i=0; i<50; i++ )
    {
    indata >> x[i];
    cout << x[i];
    }
    */
    while (count>=0 && count<=50)
    {
    indata >>number;

    cout<<"value="<<x<<endl;

    count ++

    }


    Keep on trying and you will get to what you want.

    i really recommend that if you have a text book, read it...

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    Ok, so, so far the program would look somethin like this.
    #include <iostream.h>

    void main()
    int x[50];

    indata.open ("computer.dat");
    indata >>count;

    for ( int i=0; i<50; i++)
    {
    indata >> x[i];
    cout << x[i];
    }

    while (count>=0 && count <=50)
    {
    indata>>number;

    cout<<"value="<<x<<endl;

    count++

    }

    Now for the other few parts

    3. Sort the Array into ascending order utilizing a void function.

    4. the sort function is to utilize another void function to swap 2 values.

    5. Output is to print the original data and the sorted data side-by-side.

  11. #11
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You should know that the main function is like this
    and read the comment
    #include <iostream.h>

    void main()
    int x[50];
    { //add this
    indata.open ("computer.dat");
    indata >>count; //why is this????

    for ( int i=0; i<50; i++)
    {
    indata >> x[i];
    cout << x[i];
    }

    while (count>=0 && count <=50)
    {
    indata>>number;

    cout<<"value="<<x<<endl;

    count++

    }
    } //add this also
    although according to some C++ standards you should write:
    int main()
    {
    //blabla
    return 0;
    }

    And again I Think you should declare the indata.
    Actually the way I open files is:
    ifstream indata("comouter.dat", ios::in);
    I'm not sure about your way, but I think you should add
    ifstream indata;
    but again I'm not sure.

    For the sorting part, you should use a sorting algorithm.
    for example there is the bubble sort, and the selection sort, which I think are the easiest.
    So try looking for them in your text book if you have one, look in them web.

    Note:
    Your program is fine, but it can have some modifications to become better but we will talk about it later.

    Sorry the reply was not organized, but I have a class and I don't have time.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks everyone.

    After reading many responses, I will sum commenting techniques I believe are essential and has been or will adopt.

    // I have began adopting this convention in all my projects after
    // learning MFC via Jeff Prosise a year ago.
    - Use descriptive function and variable names (Ranjan Banerji, Gary R. Wheeler, and alnite agree)

    Example: MoveCityMapRight()
    DeleteCityMapPointer()
    PaintCityMapBottomSection()

    defaultCityMap
    newCityMap
    cityMapTopSection
    cityMapCenterPoint

    - Write comments while coding (by Ranjan Banerji)

    This is a very useful commenting technique. I will definitey adopt this technique in that I add concise
    comments on algorithms and code I believe are essential as I type them. Key word is "concise."

    // This technique is basic, but I bet more programmers choose to write general comments over this technique.
    - Write comments on *why* you chose an algorithm (by Michael P Butler)

    I agree with Butler on this commenting technique. A piece of code might contain 1000 lines with 500 lines of comments.
    Nonetheless, they are meaningless unless the reader understand the fundamention of the algorithm and why it was used.
    Write a ***** analysis of the algorithm.

    - Elegant design, good and clear code top priority list (by whoie)

    This is software design and implementation wisdom!

    Kuphryn
    Last edited by kuphryn; 11-23-2002 at 10:59 AM.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    So I should take out
    indata<<count;

  14. #14
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    First I think it was indata >>count and it depends on what do you need, I mean what's the value of count that you want to read from the file?

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    I think that it is only supposed to be read 16 times

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 09:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM