Thread: Arrays

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    Arrays

    Brief Description
    1. In this assignment , you are to write a program that prints out the elements of an array of numbers and finds the aveages of those numbers. The array has as its element type int, and a size of 150.

    Design
    Your program must include these functions:
    [CODE]
    1. void intitalize (int [], int); which intializes the arrays elements to a random integer between 1 and 100.

    2. void print (int [], int); which prints out all of the array elements with 10 per line, anda spacing between the elements of 7.
    3. void print (int[], int); which prtins out all of the array elements with 10 per line, and a spacing between the elements of 7.

    This is what i have so far. Any help appreciated. The program is supposed to run in batch mode. I also have to to run the program using random numbers. He asked us to use rand and srand.


    [CODE]
    #include<iomanip>
    #include<iostream>
    #include<stdlib.h>
    #include<time.h>

    using namespace std;

    void initialize(int a[], int);

    float average(int a[], int);

    void print(int a[], int);

    main()
    {

    cout.setf(ios::fixed,ios::floatfield);
    cout.setf(ios::showpoint);

    const int size=150;
    int a[size];


    intialize(a,size)
    average(a[]);



    return 0;
    }



    void intialize(int a[], int);
    {
    int size=150;
    for(int i=0; i<size; i++)
    {
    (rand()%size)+1;
    }

    }


    float average(int a[], int);
    {
    int size=150;
    int total =0;
    for(int i=0; i<size; i++)
    {
    total += a[i];
    return float(total)/float(size);
    }
    }


    void print(int a[], int);
    {
    int size = 150;
    for(int i=0; i<size; i++)

    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    CODE TAGS
    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.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You forgot your closing code tags [/code]. (You can click the edit button to edit your post if you wish.)

    Start-out with one function (in addition to main). Maybe a function that initializes a small array. It doesn't have to be random at this point. When that compiles, and you think it's working, display the array to make sure. Then add the random feature and then the other functions a little at a time.

    Re-study the difference between a function prototype and a function definition... The prototype only needs a type (int), but the definition needs variable name (x).

    Make sure you understand how to pass a pointer to a function. You need to pass a pointer to the array, because you can't return the whole array from a function, and if pass-in the whole array, you are passing-in a copy... the original won't get updated (or initialized).
    Last edited by DougDbug; 11-25-2003 at 04:21 PM.

  4. #4
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    You need to pass a pointer to the array, because you can't return the whole array from a function, and if pass-in the whole array, you are passing-in a copy... the original won't get updated (or initialized).
    Arrays are automatically passed by reference.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Arrays are automatically passed by reference.
    Simulated reference. Arrays are passed as a pointer to the first element, but the pointer itself is passed by value, so it isn't really pass by reference.
    My best code is written with the delete key.

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    I did not know that, thanks prelude.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM