Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will do the following: Generate 20 random integers with a r

ID: 3835086 • Letter: W

Question

Write a program that will do the following:

Generate 20 random integers with a range of 1 to 10 and place them into an array of size 20.   Re-cycle the functions from Lab 10 where appropriate.

Make this a function.

Send the function the array.

Send the function the size of the array.

The data type of the function will be void since the array will be sent back through the parameter list.

Bring in the function that generates and returns a random number that you created from the previous module. Call that function from this one.

Display the contents of the array.  Re-cycle the functions from Lab 10 where appropriate.

Make this a function.

Send the function the array.

Send the function the size of the array.

The data type of the function will be void since nothing will be sent back.

After generating 20 random numbers between 1 and 10, generate one more random number (also from 1 to 10).

Search though the Array and count how many times the extra random number occurs.  It is possible that the extra random number may occur more than once or not at all.

Output:

•          Display the entire array.

•          Display the extra random number.

•          Depending upon the result of your search, display one of the following:

–         How many times the random number occurs.

–         That the random number did not occur at all.

Also include:

Use a sentinel driven outer While Loop to repeat the task

Ask the User if they wish to generate a new set of random numbers

Clear the previous list of numbers from the output screen before displaying the new set.

Write a program that adds the following to Lab 11:

• Add a function that will use the BubbleSort to put the numbers in ascending order.

– Send the function the array.

– Send the function the size of the array.

– The sorted array will be sent back through the parameter list, so the data type of the function will be void.

Output:

• Display the array after the numbers have been placed in it. It will be unsorted at this point.

• Display the array after it has been sorted.

Explanation / Answer

Program-1:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* function prototype */
void display(int[], int);
int gen_random_number(int);
void search(int[], int);

int main()
{  
   int result;
   int arr[100];
   int choice = 0;
  
   while(choice != -1){
       printf(" Menu Choices");
       printf(" ------------");
       printf(" 1.Create array with random numbers and display");
       printf(" 2.Search for an element in the array");
       printf(" Enter your choice from the above options 1, 2 (-1 to quit)? ");
       scanf("%d", & choice);
       if(choice == 1)
           display(arr, 20);
       else if(choice == 2)
           search(arr, 20);
   }

   return 0;
}

/* random number generator */
int gen_random_number(int max)
{
int result = 0;

return rand() % max;
}

/* function to display elements in array */
void display(int arr[], int size)
{
   srand(time(NULL));
   for(int i = 0;i < size; i++)
       arr[i] = gen_random_number(10);
   printf(" Array elements are:");
   for(int i = 0;i < size; i++)
       printf(" %d", arr[i]);
   printf(" ");
}

/* search an element in the array created */
void search(int arr[], int size)
{
   int count = 0, x, i;
   int flag = 0;

   srand(time(NULL));
   x = gen_random_number(10);
   for(i = 0;i < size; i++){
       if(arr[i] == x) {
           count++;
           flag = 1;
       }
   }  
    printf(" Extra random number generated is %d", x);
   if(flag)
       printf(" Element %d found in array %d times", x, count);
   else
       printf(" Element %d not found in array %d times", x, count);      
   printf(" ");
}


Execution and output:
186590cb0725:C bonkv$ ./a.out

Menu Choices
------------
1.Create array with random numbers and display
2.Search for an element in the array
Enter your choice from the above options 1, 2 (-1 to quit)? 1

Array elements are:
9
1
9
3
2
9
3
6
2
9
7
5
8
3
5
3
7
9
5
5

Menu Choices
------------
1.Create array with random numbers and display
2.Search for an element in the array
Enter your choice from the above options 1, 2 (-1 to quit)? 2

Extra random number generated is 3
Element 3 found in array 4 times

Menu Choices
------------
1.Create array with random numbers and display
2.Search for an element in the array
Enter your choice from the above options 1, 2 (-1 to quit)? -1
186590cb0725:C bonkv$


Program-2:
Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* function prototype */
void display(int[], int);
int gen_random_number(int);
void sort(int[], int);

int main()
{  
   int result;
   int arr[100];
   int choice = 0;
  
   while(choice != -1){
       printf(" Menu Choices");
       printf(" ------------");
       printf(" 1.Create array with random numbers and display");
       printf(" 2.Sort elements in the array");
       printf(" Enter your choice from the above options 1, 2 (-1 to quit)? ");
       scanf("%d", & choice);
       if(choice == 1)
           display(arr, 20);
       else if(choice == 2)
           sort(arr, 20);
   }

   return 0;
}

/* random number generator */
int gen_random_number(int max)
{
int result = 0;

return rand() % max;
}

/* function to display elements in array */
void display(int arr[], int size)
{
   srand(time(NULL));
   for(int i = 0;i < size; i++)
       arr[i] = gen_random_number(10);
   printf(" Array elements are:");
   for(int i = 0;i < size; i++)
       printf(" %d", arr[i]);
   printf(" ");
}

/* sort the given array */
void sort(int arr[], int size)
{
   int temp;

   printf(" Unsorted array elements are");
   for(int i = 0; i < size; i++)
       printf(" %d", arr[i]);
   for(int i = 0; i < size; i++)
       for(int j = 0; j < size; j++)
           if(arr[j] > arr[j+1]){
               temp = arr[j];
               arr[j] = arr[j+1];
               arr[j+1] = temp;
           }
   printf(" Sorted array elements are");
   for(int i = 0; i< size; i++)
       printf(" %d", arr[i]);
   printf(" ");
          
}

Execution and output:
186590cb0725:C bonkv$ ./a.out

Menu Choices
------------
1.Create array with random numbers and display
2.Sort elements in the array
Enter your choice from the above options 1, 2 (-1 to quit)? 1

Array elements are:
4
1
4
7
4
7
1
8
9
3
3
7
9
9
4
4
8
0
4
4

Menu Choices
------------
1.Create array with random numbers and display
2.Sort elements in the array
Enter your choice from the above options 1, 2 (-1 to quit)? 2

Unsorted array elements are
4
1
4
7
4
7
1
8
9
3
3
7
9
9
4
4
8
0
4
4
Sorted array elements are
0
0
1
1
3
3
4
4
4
4
4
4
4
7
7
7
8
8
9
9

Menu Choices
------------
1.Create array with random numbers and display
2.Sort elements in the array
Enter your choice from the above options 1, 2 (-1 to quit)? -1
186590cb0725:C bonkv$

=============================================================================

Also i have written one more 'C' program which combines above two programs. Below are the details.

Code:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

/* function prototype */

void display(int[], int);

int gen_random_number(int);

void search(int[], int);

void sort(int[], int);

int main()

{  

   int result;

   int arr[100];

  

   display(arr, 20);

   search(arr, 20);

   sort(arr, 20);

   return 0;

}

/* random number generator */

int gen_random_number(int max)

{

int result = 0;

return rand() % max;

}

/* function to display elements in array */

void display(int arr[], int size)

{

   srand(time(NULL));

   for(int i = 0;i < size; i++)

       arr[i] = gen_random_number(10);

   printf(" Array elements are:");

   for(int i = 0;i < size; i++)

       printf(" %d", arr[i]);

   printf(" ");

}

/* search an element in the array created */

void search(int arr[], int size)

{

   int count = 0, x, i;

   int flag = 0;

   srand(time(NULL));

   x = gen_random_number(10);

   for(i = 0;i < size; i++){

       if(arr[i] == x) {

           count++;

           flag = 1;

       }

   }  

    printf(" Extra random number is %d", x);

   if(flag)

       printf(" Element %d found in array %d times", x, count);

   else

       printf(" Element %d not found in array %d times", x, count);      

   printf(" ");

}

/* sort the given array */

void sort(int arr[], int size)

{

   int temp;

   printf(" Unsorted array elements are");

   for(int i = 0; i < size; i++)

       printf(" %d", arr[i]);

   for(int i = 0; i < size; i++)

       for(int j = 0; j < size; j++)

           if(arr[j] > arr[j+1]){

               temp = arr[j];

               arr[j] = arr[j+1];

               arr[j+1] = temp;

           }

   printf(" Sorted array elements are");

   for(int i = 0; i< size; i++)

       printf(" %d", arr[i]);

   printf(" ");

          

}

Execution and output:

186590cb0725:C bonkv$ ./a.out

Array elements are:

9

7

4

9

1

2

1

5

4

1

0

3

9

0

2

6

4

1

8

6

Extra random number is 9

Element 9 found in array 3 times

Unsorted array elements are

9

7

4

9

1

2

1

5

4

1

0

3

9

0

2

6

4

1

8

6

Sorted array elements are

0

0

0

1

1

1

1

2

2

3

4

4

4

5

6

6

7

8

9

9

186590cb0725:C bonkv$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote