C++ help (arrays)

Posted:
in Genius Bar edited January 2014
Can someone help me do these where I have to invoke functions to fill arrays?



First, invoke a function to fill an integer array with 100 elements with randomly picked integers ranging from -99 to 99 and return the average.



Second, I need to invoke a function that fills a 10 by 10 2 dimensional array with a floating point number (passed as a parameter).



[ 01-21-2003: Message edited by: TigerWoods99 ]</p>

Comments

  • Reply 1 of 18
    amorphamorph Posts: 7,112member
    What, specifically, do you need help with? Is there any place where you're getting stuck? This is not WeDoYourCPlusPlusHomework.com, after all.



    The hardest part of that to understand, I think, is probably how to pass the array into a function. You'll actually be passing a pointer to its first element, and since there's no way to get the size of the array from that, you'll have to pass that in too.



    If you have other particular questions, ask away. Don't be afraid to post code. And also, don't be afraid to ask your professor. That's what they're there for.
  • Reply 2 of 18
    scottscott Posts: 7,431member
    Shouldn't "array" be an object that "knows" how many elements it has, how many is has space allocated for ....



    Then you just pass a pointer to the object. Or better yet define a method for "array" object and fill it with random numbers. Each array would have it's own method to calculate "average".





    I do C and not OOP so maybe my OO thinking is off?
  • Reply 3 of 18
    K, here's my start



    #include &lt;iostream&gt;

    #include &lt;iomanip&gt;

    #include &lt;stdlib&gt;

    #include &lt;ctime&gt;



    using namespace std;



    void PauseScreen (string msg)



    int array (aiValues; 100) //integer with 100 elements

    int two_dim [10] [10]; //2D floating pt array with 10 rows 10 columns

    int i;

    int a;

    iarray a;





    int main(void)

    {



    srand (time(NULL)) //random number generator





    a = iarray_create_2D (m, n, K_FLOAT); //creates a 10x10 integer 2D iarray





    a[100] for i &lt;- 99 ton

    do A[i] &lt;- random (-99,99)



    cout&lt;&lt;"Press enter key to continue...",,endl;



    return 0;



    }



    [ 01-21-2003: Message edited by: TigerWoods99 ]



    [ 01-21-2003: Message edited by: TigerWoods99 ]</p>
  • Reply 4 of 18
    BTW, if you see this post and have some help can you IM me at supermacG9 on AIM. Thanks.
  • Reply 5 of 18
    #include &lt;iostream&gt;

    #include &lt;iomanip&gt;

    #include &lt;stdlib&gt;

    #include &lt;ctime&gt;



    using namespace std;



    void PauseScreen (string msg)



    ARRAY INTEGER (aiValues; 100) //integer with 100 elements

    int two_dim [10] [10]; //2D floating pt array with 10 rows 10 columns

    int i;

    int a;

    iarray a;

    iarray_create_2D (m, n, K_FLOAT)



    int avg = sum/counter;



    int sum, int counter;



    int sum=0



    int counter=o



    int main(void)

    {



    srand (time(NULL)) //random number generator



    a[100] for i &lt;- 99 ton

    do A[i] &lt;- random (-99,99) ///invoke function to fill integer array with 100 elements randomly picked integers from -99 to 99



    for (int i =0; i&lt;99; i++) //print out the average

    {

    \tint sum;

    \tsum += array[i];

    cout &lt;&lt; avg





    a = iarray_create_2D (m, n, K_FLOAT); //creates a 10x10 integer 2D iarray



    cin &gt;&gt; iarray_create_2D (m, n, K_FLOAT);



    for 0 to size of array; sum += array[i]; counter++; avg = sum/counter



    cout &lt;&lt; "Press enter key to continue..." &lt;&lt; endl;



    return 0;



    }



    What Im trying to figure out is after my first function how to print the integer array on 10 rows with each row showing 10 elements and how to invoke a function to exchange the largest element of the integer array with the last element of the array.



    [ 01-21-2003: Message edited by: TigerWoods99 ]</p>
  • Reply 6 of 18
    scottscott Posts: 7,431member
    Don't use "int" for the average. It should be a floating point number. You can round to an int later if you want.
  • Reply 7 of 18
    scottscott Posts: 7,431member
    oh and you may want to type cast a lot.



    float average=0;

    average=(float)sum/(float)N;





    BTW/FYI you're doing this in a C type of way. Maybe that doesn't matter.
  • Reply 8 of 18
    [quote]Originally posted by TigerWoods99:

    <strong>Second, I need to invoke a function that fills a 10 by 10 2 dimensional array with a floating point number (passed as a parameter).

    </strong><hr></blockquote>



    This part makes no sense. You can't fill a 2D integer array with a floating point number.



    ----------



    I can't give much specific advice but:



    Averaging an array is a standard beginner's assignment. If you're good with Google and you'll not just confuse yourself with what you find then think about using it.



    Break your assignment into the smallest possible chunks and make sure it works at each stage. Don't write 20 lines and then find out there's a problem. Compile constantly to find errors quickly.



    Never use magic numbers. A magic number is any value other than 0 or 1 hard coded into your program. Define constants instead.



    You should be able to change most constants without anything breaking. Your code should work for any size of array, not just 100. Smaller numbers are much easier to test. Try your code with 2 or 5 instead of 100 to see what happens.



    Break everything into functions that do one simple task. Test these independently by passing in known values and writing what is returned to the screen.



    Work out results by hand before writing functions and check them after each change. What answer should your average function return if you give it an array like 1,2,3 or 1,1,3 or 1,1,1?



    As you add a new function test that it interfaces with existing code by simply hard coding an example return value like "HELLO" or 69 and checking the rest of the code does what you expect.



    Example: if you have to print a random number then first write something that prints the number 69 every time. Once that works (i.e. average of 100 'random' numbers = 69) then you make it random.



    The 2D array is all loops within loops. Get it to work in one dimension first e.g. print 10 numbers in a line or print 1 number on each of 10 lines, then add a second loop to do the other dimension.



    Hope this helps, sorry it's not more specific.
  • Reply 9 of 18
    [quote]Originally posted by TigerWoods99:

    <strong>

    for 0 to size of array; sum += array[i]; counter++; avg = sum/counter

    </strong><hr></blockquote>



    Watch out you don't make a fence post error here.



    i will go from 0 to 99. You do the loop 100 times but the value of i never reaches 100 inside the loop because the test should be i &lt; 100 (with a constant).



    Also you divide by N where N is the number of random integers in the array. Therefore you don't need counter, you know N before you begin.



    Also note the average calculation is only done once, i.e. it is outside the loop. (Also note what Scott says about casting)
  • Reply 10 of 18
    ast3r3xast3r3x Posts: 5,012member
    i'm found of tvectors myself
  • Reply 11 of 18
    ast3r3xast3r3x Posts: 5,012member
    i'm found of tvectors myself
  • Reply 12 of 18
    scottscott Posts: 7,431member
    [quote]Originally posted by stupider...likeafox:

    <strong>



    Also you divide by N where N is the number of random integers in the array. Therefore you don't need counter, you know N before you begin.



    </strong><hr></blockquote>



    and also Make sure N!=0!!!!!!!!!! !!!!





    Are we helping too much here
  • Reply 13 of 18
    ast3r3xast3r3x Posts: 5,012member
    haha, i gave him alot of the code



    ...good thing i am not so sure it worked!
  • Reply 14 of 18
    scottscott Posts: 7,431member
    You know I haven't done much programming of late. I miss it. I've written some cool stuff IMO. Mostly number crunching optimizing stuff.



    I was talking to my "boss" today and I may be moving back toward more coding. Maybe just in Matlab though, almost doesn't count. I'll work some C in there if I have to
  • Reply 15 of 18
    ast3r3xast3r3x Posts: 5,012member
    [quote]Originally posted by Scott:

    <strong>You know I haven't done much programming of late. I miss it. I've written some cool stuff IMO. Mostly number crunching optimizing stuff.



    I was talking to my "boss" today and I may be moving back toward more coding. Maybe just in Matlab though, almost doesn't count. I'll work some C in there if I have to </strong><hr></blockquote>



    perhaps u can answer a question i have then...





    how do you optimize code for a specific processor, i mean i know altivec helps graphical stuff alot, but how do you optimize code for it...just the order of stuff or what?



    haha, my idea on how you do it



    *not optimized*

    i=i-4(x)^8





    *optimized*

    i=i-4(x)^8 [use altivec



    i dont get how u have it use that and stuff...obviously that was just a joke
  • Reply 16 of 18
    amorphamorph Posts: 7,112member
    I don't have anything to add to stupider...likeafox's excellent help, so:



    [quote]Originally posted by ast3r3x:

    <strong>



    perhaps u can answer a question i have then...





    how do you optimize code for a specific processor, i mean i know altivec helps graphical stuff alot, but how do you optimize code for it...just the order of stuff or what?

    </strong><hr></blockquote>



    There are two (large) issues here: Optimizing for the way that the CPU handles standard (integer, float, pointer, load, store) computations, and optimizing for any specialized engine (e.g. AltiVec).



    To do the former, you mostly have to know how to state and reorder problems in such a way that they play to the processor's strengths. It's hard to go into any detail about that because CPUs are all so different, but for example you can make sure that data sets fit into a processor's register set, or failing that its L1 cache.



    This is one reason why code optimized for x86 CPUs gets penalized on PPCs: One of the main ways to optimize for an x86 is to assume that you have very few registers to work with, and lots of memory bandwidth. (Registers are far, far faster than any other form of memory - access is instant.) But the PPC has lots of registers (which end up going unused) and, at least currently, not a whole lot of bandwidth to main RAM. So the code ends up wasting a lot of on-chip resources, and doing a lot of waiting. The PPCs' aggressive caching helps meliorate that somewhat, but not as well as if the code took advantage of the PPC's superior number and use of registers.



    To do the latter, you need to know the language the specialized engine speaks - it's usually a form of assembler. Apple has a nifty (and unique) macro language for AltiVec so that you don't have to code in bare assembler, but you still have to learn it and learn how to state problems in its terms, and learn how to use it effectively.



    Maximally effective AltiVec code fits into both cases, because AltiVec is so memory starved that tweaking your code to use registers and caches and downtime while waiting for a result from memory is crucial to getting the greatest amount of performance.



    However, all this is not something you worry about until you're done coding. Even then, you should only worry about it to the extent that it gets rid of irritating bottlenecks. Code which is optimized for a machine is rarely optimized for readability, and it tends to be more difficult to understand, more likely to be buggy, more likely to break, and harder to maintain than code which is a straightforward statement of the problem.



    [ 01-22-2003: Message edited by: Amorph ]</p>
  • Reply 17 of 18
    ast3r3xast3r3x Posts: 5,012member
    THANKS!
  • Reply 18 of 18
    scottscott Posts: 7,431member
    My favorite form of optimization is using a faster computer.



    My code was very very slow. I didn't care to make it fast. It just had to work well enough for me.
Sign In or Register to comment.