Jump to content


 


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.

Photo

Programming Challenge


  • Please log in to reply
20 replies to this topic

#1 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:47 AM

Posted 22 September 2008 - 07:36 PM

For the last three years I have participated in the ACM programing contest sponsored by IBM. I was also fortunate to teach a group of high schoolers some fundamental programming concepts. I thought I would share one of the programming problems that I wrote for my students. And before any Navy folk take offense. I am completely aware that bos置ns typically have little to do with gunery related issues, but the bos置n in this problem is an over-achiever. ;)

I thought it might be fun to challenge our members and see what sort of coders we have on board. On a scale of 1-10, this problem is about a 2. It is a straight-forward problem where one just needs to read the input, and plug it into a formula. You should also not that the test data I provide here is not complete. I may have a couple of test cases that one would not anticipate in the real world. So anyway, if you want, solve the problem in what ever language you prefer. As long as I can feed it the test data, the language does not matter. So here is the problem:

During one WWII battle on the Sea of Japan, the USS Missouri took damage to the system responsible for controlling the elevation of its 15 guns, locking them at a 45コ. This proves to be problematic, because the angle of the guns determines the range. At the same time, Marines are pinned down on various places and are requesting fire support.

A bos'un on board notes the following; The deck guns can launch the 2700 pound shells at a velocity of 820 m/s, with a maximum range of about 20 miles. Since the amount of gunpowder loaded with each shot determines the velocity with which the bullet leaves the barrel, by varying the amount of gunpowder, the range can be controlled, and they can save all of the Marines. The bos置n also notes that simple Newtonian physics can be used to determine with how much velocity the projectile must be launched in order to hit the target.

The first step is to calculate how much time is needed for the projectile to travel a given distance, and that is given by the following (simplified) formula:
time=sqrt(distance/(4.9))

Once the time is calculated, then a second formula can be used to determine the acceleration needed to travel that distance:
acceleration=distance/time

Finally, the velocity with which the bullet must leave the barrel can be calculated:
velocity=sqrt(acceleration^2+acceleration^2 )
*******************************************
Sample input:

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

3
1609
16090
32180

Sample output:

Velocity needed is: 125.57
Velocity needed is: 397.09
Velocity needed is: 561.57



BC AdBot (Login to Remove)

 


m

#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,301 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:47 PM

Posted 22 September 2008 - 07:46 PM

Hello Groovicus :thumbsup:

Are all the yard inputs integers or must floating points be supported as well?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 groovicus

groovicus
  • Topic Starter

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:47 AM

Posted 22 September 2008 - 07:50 PM

Are all the yard inputs integers or must floating points be supported as well?

That is a good question, isn't it. If you were to program this for a real world instance, what would you do?

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,301 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:47 PM

Posted 22 September 2008 - 07:56 PM

Hello again Groovy :thumbsup:

Should I just post my answer here or PM or what?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 groovicus

groovicus
  • Topic Starter

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:47 AM

Posted 22 September 2008 - 07:59 PM

Go ahead and post it here. This is no super-secret deal. Just as long as I can test it is all.

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,301 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:47 PM

Posted 22 September 2008 - 08:01 PM

Hello for a third time :thumbsup:

Here you go. I chose C++.

#include <iostream> //required for console i/o
#include <cstdlib> //required for EXIT_SUCCESS macro, endl
#include <cmath> //required for sqrt()
using namespace std;

int main()
{
	//initialize a variable to hold the number of shells we want
	int numshells;
	//initialize a pointer to hold a dynamic array
	double *yardinput;
	cout << "How many shells are you firing?\n";
	cin >> numshells;
	//allocate the array large enough to hold the number of shells you want
	yardinput = new double[numshells];
	//ask the user for x amount of variables and populate the array with them
	for (int curshell = 0; curshell < numshells; curshell++) {
		cout << endl << "Input shell number: " << curshell + 1 << " ";
		cin >> yardinput[curshell];
	}
	cout << endl << endl << "Result: ";
	//loop though the variables we have and calculate the answers
	for (int curshell = 0; curshell < numshells; curshell++) {
		cout << endl << "Shell " << curshell + 1 << ": ";
		double time = sqrt(yardinput[curshell] / 4.9);
		double a = yardinput[curshell] / time;
		// ^ operator is not supported on doubles, so I multiplied it by itself to square it
		double v = sqrt(2*(a*a));
		cout << v;
	}
	//free the ram we used with the array
	delete [] yardinput;
	cout << endl << endl << "Done.";
	//wait so the user can see the result
	system("pause > nul");
	return EXIT_SUCCESS;
}

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 groovicus

groovicus
  • Topic Starter

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:47 AM

Posted 22 September 2008 - 08:05 PM

Ok. The only probelm is that it needs to read the input from a file called blast.txt.

Typically in a programming contest, all of the input is provided in the form of a text file. There is no way I am going to type in 1000 numbers just to test your solution.

Expect to get some constructive criticism also about coding practices. :thumbsup: No charge.....

EDIT: And it should go without saying that if you input my data that I provided, you should get the same output, and it should be formatted exactly as my output.

#8 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,301 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:47 PM

Posted 22 September 2008 - 08:51 PM

Sweet :thumbsup: Always trying to get to be a better programmer.

Attempt #2;

//Version 2:
#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;

//NOTE: This funciton assumes that all numbers are less than 16 digits long, as this is the range of a double. (On x86 systems)
//Change this define if you need that changed

#define doublelength 16

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.
	int numcases;
	char currentline[doublelength + 1]; //double max length plus one for null terminator
	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);
	input.getline(currentline,doublelength + 1);
	numcases = atoi(currentline);
	for (int curcase = 0; curcase < numcases; curcase++) {
		double currentnumber;
		input.getline(currentline,doublelength + 1);
		//convert the string to a double
		currentnumber = atof(currentline);
		output << "Velocity needed is: " << sqrt(2 * pow(currentnumber / sqrt(currentnumber / 4.9),(double) 2)) << endl;
	}
	input.close();
	output.close();
}

EDIT: Sorry, I forgot. A compiled version is here: http://billy-oneal.com/BleepingComputer/Gr...nge20080922.exe

Billy3

Edited by Billy O'Neal, 22 September 2008 - 09:14 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#9 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:47 AM

Posted 22 September 2008 - 10:38 PM

The first step is to calculate how much time is needed for the projectile to travel a given distance, and that is given by the following (simplified) formula:
time=sqrt(distance/(4.9))

Once the time is calculated, then a second formula can be used to determine the acceleration needed to travel that distance:
acceleration=distance/time



In high school newtonian physics:

Acceleration = distance / (time)^2
OR
Acceleration = velocity / time

So would you please check the problem again?

Edited by Romeo29, 23 September 2008 - 05:36 AM.


#10 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:47 AM

Posted 23 September 2008 - 05:33 AM

If you do a little maths you will arrive at this simplified formula:

velocity needed = sqrt(9.8*distance)

using this formula and C as programming language I wrote the following program. Not much commented for its simple enough. I havent used security functions. C because Billy O'Neal took C++ :thumbsup:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[]){
	FILE * f;
	char szLine[10];
	float distance;
	char *stop;

	/*if no arguments given*/
	if(argc!=2){
		printf("Please specify data file!\nUsage: %s [filename]\n",argv[0]);
		return 1;
	}
	/*if file in argument cannot be opened*/
	if((f = fopen(argv[1],"r"))==NULL){
		printf("Error: Unable to open data file!\n");
		return 2;
	}
	fgets(szLine, sizeof(szLine), f); /*discard first line*/
	while(!feof(f)){
		/*read into string*/
		if(fgets(szLine, sizeof(szLine), f)==NULL) break;
		/*convert into float*/
		distance = strtof(szLine, &stop);
		if(distance>0){
			/*calculate and print velocity*/
			printf("Velocity needed is: %0.2f\n", sqrt(9.8*distance));
		}
	}
	fclose(f);
	return 0;
}

Edited by Romeo29, 23 September 2008 - 05:35 AM.


#11 groovicus

groovicus
  • Topic Starter

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:47 AM

Posted 23 September 2008 - 05:42 AM

:thumbsup: Programming problems can often be simplified.

I will test the programs later.

#12 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,301 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:47 PM

Posted 23 September 2008 - 02:46 PM

@Romeo29:
The A = V/T only works when you are dealing with metric equivalent units. Since we are using yards as our input, and meters/s as our output, you can't use that function. Using that would not be dimensionally correct.

Given the task at hand, we need to use the function given to us ;)

The simplification to sqrt(9.8*d) does work though :thumbsup:

@Groovy:
This is better code which removes the #define doublelength, as well as handles your "Multiple Test Cases" scenario:
#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);

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: " << sqrt(9.8*curnum) << 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();
		}
}

A binary for the above is here: http://billy-oneal.com/BleepingComputer/Gr...922Attempt3.exe

Thanks for the challenge :flowers:

Billy3

EDIT: Fixed syntax error

Edited by Billy O'Neal, 23 September 2008 - 03:05 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#13 groovicus

groovicus
  • Topic Starter

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:47 AM

Posted 23 September 2008 - 02:50 PM

I still have not had time to test. I am trying to prepare a presentation for tonight. :thumbsup:

I keep getting side-tracked.

#14 groovicus

groovicus
  • Topic Starter

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:47 AM

Posted 26 September 2008 - 04:58 PM

Billy; Incorrect output.

Romeo: Incorrect

(If both programs were combined, then it would be perfect ;) )

#15 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:47 AM

Posted 26 September 2008 - 09:49 PM

Would you please post a working solution in any language you prefer and a sample data file to be tested with?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users