Randomly generate 50 integers within the range of 0 – 999.
Question
C language
Randomly generate 50 integers within the range of 0 – 999. Store the integers in an array. Pass the array to different functions. That perform the tasks below:
i. So Print out the randomly generated integers in 5 rows, each row having 10 integers.
Input: array.
return: none.
Random integer generation:
#include<stdlib.h>
rand(); //generate random integer.
rand()%999; //generate random integer between 0-999.
ii. And Show the range of integers generated: Output the smallest and largest integer and their index in the array.
Input: array.
return: none.
iii. And Sort the integers in ascending order.
input: array.
return: none.
findsmallestnumber(int start, int end, array)
input: start index of array, end index of array, array.
return: smallest number index or position in array.
iv. And Generate another array of 50 integers. Compare the 2 arrays and count the similarity percentage of the 2 arrays.
Input: Array1, Array2
Return: none.
** Use 2 for loops for this function. One to loop Array1 and another one inside to loop Array2.
Sort Algorithm Pseudocode:
sort(array)
{
loop(array start to end)
{
smallestNumberIndex = findsmallestnumber(array, currentindex, end);
if (currentIndex != smallestNumberIndex)
{ //swap places.
temp =array[smallestNumberIndex];
array[smallestNumberIndex] = array[currentIndex];
array[currentIndex] = temp;
}
}
Summary
In this question, we have to write a c program. To randomly generate 50 integers within the range of 0 – 999. So we will store the elements of this into an array. And we have already given the function names.
Explanation
We use different functions and its definition in this program. So first we start with the header files. Then we insert the functions like print, range, and sort. And Then there is the main function. After that, we insert the for loop to create the array of 50 numbers. And then it will compare. Also will find which one is the smallest. And which one is greatest. Once all this is done it will get printed on the screen as output. So we will have our output.
Code
#include<conio.h> #include<stdio.h> #include<stdlib.h> //function prototypes void print(int arr[], int length); void range(int arr[], int length); void sort(int arr[], int length); int findSmallestNum(int arr[], int length); void compare(int a1[], int a2[], int length); void main() { int a[50], b[50], i; //generate array 1 of 50 random numbers for(i=0; i<50; i++) a[i]=rand()%1000; //calling all the methods printf("\n1st generated array "); printf("\nOutput arry items: "); print(a,50); range(a,50); printf("\n\nSorted Output array items: "); sort(a,50); print(a,50); //it will give us the array for(i=0; i<50; i++) b[i]=rand()%1000; printf("\n\n2nd generated array "); printf("\nOutput arry items: "); print(b,50); compare(a,b,50); } //to print the numbers void print(int arr[], int length) { int i; for(i=0; i<length; i++) { if(i%10==0) printf("\n"); printf("%d\t",arr[i]); } } // print the numbers void range(int arr[], int length) { int max=0,i; for( i=1; i<length; i++) { if(arr[max]<arr[i]) max=i; } printf("\nSmallest int: %d\tIndex: %d",arr[findSmallestNum(arr,50)],findSmallestNum(arr,50)); printf("\nLargest int: %d\tIndex: %d",arr[max],max); } //function to sort array void sort(int arr[], int length) { int temp,i,j; for(i=0; i<length; i++) for(j=0; j<length; j++) if(arr[i]>arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } //which is the smallest int findSmallestNum(int arr[], int length) { int min=0,i; for(i=1; i<length; i++) { if(arr[min]>arr[i]) min=i; } return min; } //it will compare two numbers void compare(int a1[], int a2[], int length) { int count=0,i,j; for(i=0; i<length; i++) { for(j=0; j<length; j++) if(a1[i]==a2[j]) { printf("\n%d is smaller",a1[i]); count++; } } printf("\n%d similar",count); printf(" %2f similarity",(float)(count*2.0)); }
Output