Project 1

Essay > Words: 1034 > Rating: Excellent > Buy full access at $1

[Author Name]

[Instructor Name]

[Class/Unit/Course]

[Date: dd month year]

Project 1

The program aims at generating random numbers and writes them on a file. It also counts the number of comparisons made.

Code

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <time.h>

#include <fstream>

using namespace std;

static int compares =0;

int main()

{

int I,j;//counters

int random;

bool inputloop;

int user_entry;

cout<<”How many entries? (0 – 20000)”<<endl;

cin>>user_entry;

do{

if(user_entry > 20000 || user_entry < 0){

compares++;compares++;

inputloop = true;

cout    << “Range should be 0 – 20000. Please try again”<<endl;

cin     >> user_entry;

}

else{inputloop = false;}

}while(inputloop);

int *numbers   = new int[user_entry];

int *memory    = new int[user_entry];

srand(time(NULL));

for(I =0 ; I <user_entry ; i++){

random  = rand() % user_entry +  1;  //1 – max

if(memory[random -1] == 1 ){       //skip indexing if already

i–; compares++;                 //subtract the counter to continue looping

}else{

numbers[i] = random;

memory[random – 1] =1;

}

}

ofstream output;

output.open(“unsorted.txt”);

int lines;

if (user_entry%10==0)lines=user_entry/10;

else lines=user_entry/10+1;

compares++;

for (j=0; j<lines;j++) {

compares++;

for ( i=0;(i<10) && (10*j+i<user_entry);i++) {

compares++;compares ++;

output << setw(7)<< numbers[10*j+i];

}

compares++;

output << endl;

}

compares++;

cout<<”Written to file”<<endl;

output.close();

output.flush();

cout <<endl<< “compares “<< compares<<endl;

return 0;

}

ofstream output;

output.open(“unsorted.txt”);

int lines;

if (user_entry%10==0)lines=user_entry/10;

else lines=user_entry/10+1;

compares++;

for (j=0; j<lines;j++) {

compares++;

for ( i=0;(i<10) && (10*j+i<user_entry);i++) {

compares++;compares ++;

output << setw(7)<< numbers[10*j+i];

}

compares++;

output << endl;

}

compares++;

cout<<”Written to file”<<endl;

output.close();

output.flush();

cout <<endl<< “compares “<< compares<<endl;

return 0;

}

Project 2

This entailed the reading of randomly arranged numbers in a file, therefore, using the bubble sort algorithm to sort them in order. The program al.............


Type: Essay || Words: 1034 Rating || Excellent

Subscribe at $1 to view the full document.

Buy access at $1
CategoriesUncategorized