Jump to content

Drivers and Arrays.... is this right?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
OK... drivers and arrays are hard!!!!!!!

i think this code may be right, but will someone check it to make sure it looks ok?

here is the project

Quote

Write a function that will fill an array with 25 random numbers between 1 and 10. Then write a function that will print out the array. Write a driver that will test your functions

Do not use global variables here. Pass the array to the function through the parameter list





here is the code
#include <iostream>  

#include <ctime>  

using namespace std;  

  


void printFunc(int[]);  

void fillFunc(int[]);  

  

int main()  

{  

int random[25]; //0-24 is 25 since array indeices  

srand((unsigned)time(NULL));  

fillFunc(random);  

printFunc(random);  

return 0;  

}  

  

void fillFunc(int arr[])  

{  

        for (int i = 1; i < 25; i++)  

        {  

                    arr[i] = 1+ rand() % 10;  

         }  

  

}  

  

void printFunc(int arr[])  

{  

          for (int i = 1; i < 25; i++)  

          {  

                    cout << arr[i];  

         }  

}  



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Two quick questions:
1) does it compile?
2) does it appear to run correctly?

I suspect your output (assuming it compiles/runs) will immediately make one limitation on your code apparent. I would also read up on the rand() function.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
1) yes it compiles
2) it runs....... idk about correctly?

theres no spaces between the numbers, it looks like this 1101328448174183742547656 Press any key continue....

whats the limitation you speak of?

thanks for your help

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,710 posts
Change this:
void fillFunc(int arr[])  
{  
        //start indexing from 0, not 1. arrays start at 0.
        for (int i = 0; i < 25; i++)  
        {  
                    arr[i] = 1+ rand() % 10;  
         }  
  
} 
void printFunc(int arr[])  
{  
          //you're always skipping the first element. arrays
          //are always started counting at 0.
          for (int i = 0; i < 25; i++)  
          {  
                    //put spaces between numbers
                    cout << arr[i] << " ";  
         }
         cout << endl;
}

You were very close to a good implementation; you just needed to put spaces between the numbers and an endl at the end to shift "Press any key to continue..." to the next line. Just remember that arrays are always indexed from 0, not 1.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Whenever you're producing output, you want to make sure it is readable (such as adding spaces like dargueta suggested). When you have a question on your program, providing the output and any behaviors of concern can be a great help. When dealing with random numbers, do a few runs to verify that the output appears to be random (I had a Tetris game that didn't initialize the RNG, so I always got the pieces in the same order).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
thank you so much for the help everyone! that ran perfect, such little stupid mistakes i do. :)

#7
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Looks like you're getting better at coding Hawk1. Keep it up!

#8
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
thanks steve, its the hardest thing i've ever had to do, but thank goodness for this forum to help me get through it.