Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

Programming Challenge


  • Please log in to reply
20 replies to this topic

#16 Billy O'Neal

Billy O'Neal

    vcpkg Maintainer


  •  Avatar image
  • Malware Response Team
  • 12,305 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:12:44 PM

Posted 27 September 2008 - 12:32 AM

Looking back over things.. I don't see anywhere where I could be making bad results. I checked this function against the sample problem provided.

Only thing I can think of is that my program currently reports one more decimal than in your sample output. This revised version clips the resultant output to two decimal places.

#include <iostream> //provides support for fstream. Also used for printing errors to console.
#include <fstream> //required for file i/o
#include <cstdlib> //required for EXIT_SUCCESS macro, atoi(), atof(), endl, pow()
#include <cmath> //required for sqrt()
using namespace std;

void readToContent(ifstream *input);
double fix(double inputnumber, int decimals);

int main(int argc, char * argv[]) {

	//For the sake of brevity, I have left off exception handling for now. This function assumes input is formatted correctly.
	if (argc != 3) {	
		cout << "Syntax:\nprogram.exe NameOfInputFile NameOfOutputFile\n\nTerminating.";
		return 1;
	}
	ifstream input;
	ofstream output;
	//open the first argument for reading
	input.open(argv[1],ios::in);
	//open the second arg for writing
	output.open(argv[2],ios::out);
	//The stream will evaluate to false end of file when the end of file is reached.
	while(input) {
		int numcases;
		input >> numcases;
		for(int curcase = 0; curcase < numcases; curcase++){
			double curnum;
			input >> curnum;
			output << "Velocity needed is: " << floor( (100*sqrt(9.8*curnum)) + .5)/100  << endl;
		}
		//Read to content to force loop exit if there is whitespace ending the file.
		readToContent(&input);
		//Sets of test cases delmited with a blank line
		if (input) {output << endl;};
	}
	input.close();
	output.close();
}

void readToContent(ifstream *input) {
		//Skip past whitespace
		char temp;
		do {
			temp = (char) input->get();
		} while ((temp == '\n' || temp == ' ') && input);
		//once we have read non whitespace, decrement so that is read again for >> later
		if (input) {
			input->unget();
		}
}

Compiled version here:
http://billy-oneal.com/BleepingComputer/Gr...922Attempt4.exe

Thanks!

Billy3

BC AdBot (Login to Remove)

 


#17 groovicus

groovicus
  • Topic Starter

  •  Avatar image
  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:44 PM

Posted 27 September 2008 - 06:04 AM

Only thing I can think of is that my program currently reports one more decimal than in your sample output.


Correct. :thumbsup:

#18 Romeo29

Romeo29

    Learning To Bleep


  •  Avatar image
  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:03:44 PM

Posted 27 September 2008 - 10:06 AM

I could not find any problem with my program.

It is working on my computer.

Have a look : http://www.jondlar.co.cc/bleep.html

groovicus would you please explain, how my program is incorrect. Its no use jusy saying "incorrect" if you dont explain, and I dont learn anything.

#19 Billy O'Neal

Billy O'Neal

    vcpkg Maintainer


  •  Avatar image
  • Malware Response Team
  • 12,305 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:12:44 PM

Posted 27 September 2008 - 11:44 AM

Romeo29, your program fails under the following test case:

3
434
234
234
2
342
324

This should be output as:

Velocity needed is: 65.22
Velocity needed is: 47.89
Velocity needed is: 47.89

Velocity needed is: 57.89
Velocity needed is: 56.35

from the challenge:

The input will consist of an integer representing the number of test cases, followed by the necessary distance, in yards.

The program should only loop over the number of test cases asked for. It should also check to see if any more test sets exist in the sample data before termination.

For that test case your example currently prints as
Velocity needed is: 65.22
Velocity needed is: 47.89
Velocity needed is: 47.89
Velocity needed is: 4.43
Velocity needed is: 57.89
Velocity needed is: 56.35

Hope that helps,
Billy3

#20 Romeo29

Romeo29

    Learning To Bleep


  •  Avatar image
  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:03:44 PM

Posted 27 September 2008 - 02:37 PM

good point!

I had not thought that a person would want to put more distances in the data file and analyze fewer only.

I am dumb :thumbsup:

#21 groovicus

groovicus
  • Topic Starter

  •  Avatar image
  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:44 PM

Posted 27 September 2008 - 02:38 PM

Under programming contest conditions, you get one of three responses; incorrect, incorrect output, or correct. I was trying to simulate those conditions. Technically, both programs still fail because not all of the requirements were met.

Romeo, I thought that bay saying "if both programs were combined, it would be correct," was a good enough hint :thumbsup:

EDIT: And you are not dumb.....




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users