Write a program to accomplish the following tasks:
1. Generate arbitrary number of random integer numbers between [0-100]
2. Save or load the numbers from/to a text file based on user option
3. Search the generated list for an arbitrary number entered by a user
4. Use Linear and Binary search algorithms based on a user option
5. Computes CPU time used by either search algorithm
6. Use menu-based interaction with the user

This is my work but I believe I did it wrong

1 {
2 int rNumb[50];
3 input (rNumb);
4 output (rNumb);
5 }

6 int input (int x[])
7 {
8   int i = 0;
9   srand(time(0));
10  for(int i=0; i<50; i++) 
11    x[i] = rand()%100+1; 
12  return x[i];
13 }


int output (int x[])
{
    int i = 0;
    for (int i = 0; i < 10; i++)
    {
    if (x[i] % 2 == 1)
         cout << x[i] << " ";
    }
    return x[i];
}

This is my work but I believe I did it wrong
1 {
2 int rNumb[50];
3 input (rNumb);
4 output (rNumb);
5 }

6 int input (int x[])
7 {
8 int i = 0;
9 srand(time(0));
10 for(int i=0; i<50; i++)
11 x = rand()%100+1;
12 return x;
13 }


int output (int x[])
{
int i = 0;
for (int i = 0; i < 10; i++)
{
if (x % 2 == 1)
cout << x << " ";
}
return x;
}

Next time wrap your code it in code tags, it automatically adds number lines
Now for your code...

return x[i];

return statements are not needed, While passing arrays to functions, the address of the array is passed from the calling function. Since the address of the array is always passed to a function, it is easy to alter the contents of the array directly in the called function.

This is my work but I believe I did it wrong

It's definitely wrong, but is this your whole program? If so, I would start again and have a really good think about the steps that you have to break your code down into before you attempt to write any code. I do this by writing a series of comments and then add the code in later. For example, you know that your program must contain a main function, so I'd start there:

int main()
{
   return 0;
}

OK, now write all the stages that you need to perform:

int main()
{
   /* Generate arbitrary number of random integer numbers between [0-100] */

   /* Save or load the numbers from/to a text file based on user option */

   /* Search the generated list for an arbitrary number entered by a user */

   /* Use Linear and Binary search algorithms based on a user option */

   return 0;
}

I've left out the menu-based stuff, you can add that in later. Now, you can add more comments to break each of these stages down into smaller stages:

int main()
{
   /* Generate arbitrary number of random integer numbers between [0-100] */
      // Make an array/vector to store the random numbers
      // Call a function that populates an array with random numbers

   /* Save or load the numbers from/to a text file based on user option */
      // Ask the user if they want to save the numbers, or load old numbers from file
      // IF SAVE
         // Call SaveNumbers function
      // IF LOAD
         // Make an array/vector to store the numbers from the file
         // Call LoadNumbers function

   /* Search the generated list for an arbitrary number entered by a user */

   /* Use Linear and Binary search algorithms based on a user option */

   return 0;
}

I've done the first two, I'll leave it to you to take it from there. Just keep breaking the process down until it's obvious to you how to code the next step. You can do the same thing for the functions that you need to call too:

bool SaveNumbers( const char* pszFilename, int* piNumbers, int iSize )
{
   // Open file and check that it opened OK
      // If it didn't open, return false
   
   // For each number in the array
      // Write number to file
      // If there was some kind of problem, return false

   // Close the file
   
   // return success!      
}

By planning your code like this, without actually writing any code, you can see some of the problems that might arise with any particular design that you have. It can save you time in the long-run. Hope that helps

Thank you, you both have been such great help to me. It makes more sense now =]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.