Write a program in C language with the following criteria. Name the program acco
ID: 3548233 • Letter: W
Question
Write a program in C language with the following criteria.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 100
// linear search function to search in array.
int linear_search(int account_array[],int random_count,int search_account_num)
{
int i;
// process for each and every value in array.
for(i=0; i<random_count; i++)
{
// if account was found return index
if(search_account_num==account_array[i])
{
return i;
}
}
// if code reached here means account was not found. return -1;
return -1;
}
// selection sort to sort account numbers.
void selectSort(int list[],int n)
{
int toFill,smallPos,pos,temp;
//STEP 1: repeat each position toFill from 0 to n-2
for(toFill=0; toFill<=n-2; toFill++)
{
//STEP 2.1:intialize smallPos to toFill
smallPos = toFill;
// STEP 2.2: repeat pos ranging from toFill to n-1
for(pos = toFill; pos<=n-1; pos++)
{
//STEP 2.3: if list[pos] is smaller than list[smallPos]
if(list[pos]<list[smallPos])
//STEP 2.4 copy pos to smallPos
smallPos = pos;
} // end for
// STEP 3: if smallPos not equal to toFill swap list[smallPos] to list[toFill]
if(smallPos!=toFill)
{
temp = list[smallPos];
list[smallPos] = list[toFill];
list[toFill] = temp;
}
} //end for
}
int main()
{
// FILE pointer to open file.
FILE* foutp;
// variable to hold random count
int random_count;
// array to hold account numbers.
int account_array[MAX],i;
// temporary variable to hold account number.
int account_num;
// variable to hold account number to search for.
int search_account_num;
// seed rand with null.
srand(time(NULL));
// variable to hold result of linear search.
int index;
// generating random _ count.
random_count = rand()%MAX + 1;
// open file to write.
foutp = fopen("accounts.txt","w");
// print random count generated.
printf("Random value genearated is %d",random_count);
// now start generating account numbers.
for(i=0; i<random_count; i++)
{
account_num = rand()%89999 + 10000;
// wirte to file account number/
fprintf(foutp,"%d ",account_num);
// print account number on to the screen
printf(" Account Number is : %d",account_num);
// put the same into array.
account_array[i] = account_num;
}
// close file.
fclose(foutp);
// ask the user account number to search for.
printf(" Enter account number to search for ");
scanf("%d",&search_account_num);
// call linear search fucntion to find account number found ornot.
index = linear_search(account_array, random_count, search_account_num);
// if account number was found print the index.number
if(index>=0)
printf(" Account was found at index: %d",index);
// print that account was not found.
else
printf(" Account was not found");
// call selection sort..
selectSort(account_array, random_count);
// now print account number
for(i=0; i<random_count; i++)
{
printf(" Account Number is : %d",account_array[i]);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.