Below are the instructions from my professor. I am programming in C. Any help is
ID: 3749776 • Letter: B
Question
Below are the instructions from my professor. I am programming in C. Any help is appreciated, thank you! You can ignore 9 and 10, but if you could give a little help on how to go about number 9 that would be helpful.
Write a program to search an array of random integers:
1. Include <stdio.h> and <time.h>
2. Define a macro MAXZISE of 100 for array size
3. Include argc and argv as the arguments of the main function
4. Declare variables in the main function:
an integer array of MAXSIZE
an integer variable for loop index
an integer variable to store the command line input
5. Set the seed for the random number generator using the time()
6. Check argc to make sure there is a command line argument
If there is no command line argument, display a message, then exit
7. Convert the command line argument to an integer value and save it in the variable declared in 4
8. Assign random number between 0 and 1000 to the entries in the array
9. Call the search function to search the array to find the position of the number entered by the user at the command line. The executable code of the search function is located at
~slt8884/cs3500/examples/array_search.o
The search function has the following prototype:
int search(int arr[], int n, int x)
// where n is the size of the array;
// if x is present then return its location, otherwise return -1
10. Assume your program name is search_array_of_random_numbers.c
Then you can compile the program by the command:
gcc search_array_of_random_numbers.c ~slt8884/cs3500/examples/array_search.o
Explanation / Answer
I wrote a Program in order to make you understand the Command Line Arg Concepts.
The stated Code might get fail due to platform dependency but it will help you to understand the implementation.
// WAP for the Chegg Answers /
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<time.h>
#define MAXSIZE 100
int main(int argc, char *argv[])
{
int arr[MAXSIZE];
int loop;
int num;
if(argc<3) // To make sure the Command Line is working as expected.
{
printf("******************** Not a Command Line Input Recieved ****************************");
exit(1);
}
num = argv[0] - '0'; // COnversion from String to Int
srand(time(0)); // Setting Seed for the Random Number Generator.
for(loop=0;loop<MAXSIZE;loop++)
{
arr[loop]=rand();
}
// search(int arr[], int n, int x);
/* AS you said the Search function is feasible for you to implement I rest my case with pleasure */
}
Please Appreciate if you liked the answer and resolved your quench.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.