Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
Hey guys,
It's crunch time for college students, good luck to you all! If any of you have time to help, it would be greatly appreciated. Here's my project:
Program Description: Write a program that calculates the intensity of light onto a NBA basketball court. A standard NBA basketball court is 94 feet long and 50 feet wide. We will assume that every square foot is represented by a cell in a two-dimensional array (or matrix). Given certain criteria that you read in from a data file, you will need to calculate the intensity of light at each square foot of the court.
Your program must have the following 4 functions:
A. Calculate the intensity of light (see formulas below)
B. Calculate the distance from the light to the floor
C. Random number generator
D. Print out the results
Formulas: Fc=Cp/D^2 D=sqrt(b^2+h^2)
Where: Fc = is the level of light the candle (floodlight) produces
D = the distance the lamp is from the surface
Cp = the candle power of the lamp (in watts)
Use the second formula to calculate the distance (D) from the light to each position (cell) on the basketball court.
Here's the data file and what each number/character gives:
Data file:
A
2
G
25 23.5
25 70.5
400
20
B
2
G
25 23.5
25 70.5
1000
30
C
3
R
400
35
What each line means:
o Option letter (to be echoed back out)
o Number of floodlights installed
o Character to determine if the measurements are (G)iven or ®andom. (If random then you need to randomly produce the position of that number of floodlights.)
o For each given floodlight, the number of feet across the court (in the 50 direction) then the number of feet (in the 94 direction)
o Power of the floodlights in watts
o Distance (feet) the lights are mounted above the positions
Here's where I need your help. I'm pretty good with functions and inputting the data from the file, I'm just terrible at arrays. After I calculate the intensity of light(Fc), how do I put these values into an array? Any help with this at all would be greatly appreciated. I need more help with the array part of it, but I could use tips throughout the whole thing. Thank you!
Posting Freak
Posts: 3,054
Threads: 142
Joined: 2008-07
Gender: Male
Country Flag: Massachusetts
IGN: flarfek
Server: Scania
Level: 150
Job: ILAM
So first thing, you need to initialize the array. Fortunately, this is pretty simple, as you already know the size of both dimensions.
Code: float[94][50] court;
Should probably do it unless I'm mixing up c++ syntax. The next step is populating the array with values - which should be pretty simple to do once the data is loaded in.
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
Okay thanks! I have a question on reading in the data from the file
as you can see from above, Option A and Option B both have two floodlights with given measurements. But Option C has three floodlights all with random values. how should I read the measurements in? Right now i have
Code: lights >> option >> num_floodlights >> given_or_random >> measurement_1 >> measurement_2 >> measurement_3 >> measurement_4 >> power >> height;
But as soon as it loops around for the last option, C, my data reading in will get messed up..
could i do this?: Code: while(option !=C)
lights >> option >> num_floodlights >> given_or_random >> measurement_1 >> measurement_2 >> measurement_3 >> measurement_4 >> power >> height;
then do all the calculations and stuff in there then do a separate while loop for option C, like:
Code: while(option=C)
lights >> option >> num_floodlights >> given_or_random >> power >> height;
Posting Freak
Posts: 3,054
Threads: 142
Joined: 2008-07
Gender: Male
Country Flag: Massachusetts
IGN: flarfek
Server: Scania
Level: 150
Job: ILAM
What would you do if you're given a file where A or B are random, or C is given? The important factor for deciding which values need to be loaded is whether the values are given or random.
For that matter, what does your code do if you add another light to A or B?
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
I honestly have no idea…I'm at a loss right now. Could you look at all the code I have right now and tell me where I went wrong with the reading in of data?
Posting Freak
Posts: 3,054
Threads: 142
Joined: 2008-07
Gender: Male
Country Flag: Massachusetts
IGN: flarfek
Server: Scania
Level: 150
Job: ILAM
What's the text parser you're using? I've never seen that syntax - I could help more if I knew what exactly I was looking at.
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
oh oops, sorry! I'm using Microsoft Visual Studio 2012.
Posting Freak
Posts: 3,054
Threads: 142
Joined: 2008-07
Gender: Male
Country Flag: Massachusetts
IGN: flarfek
Server: Scania
Level: 150
Job: ILAM
So take things one at a time: First, differentiating between given and random. You know that up until that point, all the data is going to be the same. That line determines what will follow, though. If it's given, then you'll have to do one thing; if it's random, another.
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
hmmm, okay. I kind of have an idea of what to do, but we'll see if I can get it to work  thank you for all the help
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Are you allowed to use the standard library?
i.e. std::array or std::vector
Posting Freak
Posts: 1,359
Threads: 12
Joined: 2011-04
are you allowed to write your own header files for class definitions? or are you just working with an array of like structs or something where the struct would be:
struct LightingMode
{
char option;
int floodlightCount;
char givenOrRandom;
double floodlightDistance[floodlightCount]; //its been awhile since i worked with arrays that aren't in a class without a constructor, not sure if syntax is legal
int/double floodlightPower;
double floodlightHeight[floodlightCount];
}
//calculation functions goes here
as for putting the values in the array, after calculating the value of light in a 2d cell, store the value in that arrays cell using a double for loop. for example
double courtCells[cellHeight][cellWidth];
LightingMode first;
for(int i = 0; i < cellHeight; i++)
{
for(int j = 0; j < cellWidth; j++)
{
courtCells[i][j] = Function1(First ,Function2(first));
}
}
where function 1 is the light level(that returns a double) whose parameter is a LightMode struct and an int value(distance), which is calculated by passing the LightMode struct into Funtion2(the distance calculating function whose parameter is a struct and returns an int)
at least i think, not sure how far in your class youve gotten into in C++
using a while(fin.good()) loop or similar loop, I would input the data into the struct directly with fin(or whatever you decide to name it to the point where you get to
fin >> givenOrRandom;
where there's one if/else statement where it checks the char where its either given or random. so you must do one or the other
if(givenOrRandom == C) do something
else do the other thing
I would personally have done this in a class header file(given my current knowlege, as a constructor could easily just do all the work for you)
Posting Freak
Posts: 3,054
Threads: 142
Joined: 2008-07
Gender: Male
Country Flag: Massachusetts
IGN: flarfek
Server: Scania
Level: 150
Job: ILAM
^^^ I basically agree with everything in this post. If you've never done anything with header files, don't worry about it - you should be able to do everything you need to without involving another layer of complication.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
There is no reason to make a class for this. These are mathematical pure functions and they don't require classes.
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
[MENTION=7833]Dudewitbow[/MENTION];
I don't believe were allowed to use and write our own header files, seeing that we've never learned about that! But this is a huuuuuge help, thank you so very much kind sir! this will shave off a few hours of my all nighter
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
okay so this is what I have so far, still need some help unfortunately
comments are also included! Those big starred boxes are function comment blocks, don't know if thats a universal thing or just something my teacher likes to do
Code: #include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream> //required for file streaming
#include <cctype>
#include <ctime>
using namespace std;
/************************************
Function Name: Rand_Float
This function generates a random value
between a and b.
Passed: a = min value
b = max value
Returned: random double value
************************************/
double Rand_Float(double a, double b)
{
return ((double)rand()/RAND_MAX)*(b-a) + a;
}
/*************************************
Function Name: Distance
This function calculates the distance from
the light to the floor
Passed: measurement_1 = base
measurement_2 = height
Returned: distance
**************************************/
double Distance(double measurement_1, double measurement_2)
{
double distance;
distance = sqrt(measurement_1*measurement_1+measurement_2*measurement_2);
return distance;
}
double Intensity(int power, double Distance)
{
double intensity;
intensity = power/(Distance*Distance);
return intensity;
}
int main()
{
//Declare objects
srand(time(0));
char option;
int num_floodlights;
char given_random;
double measurement_1, measurement_2, measurement_3, measurement_4;
double distance;
double intensity;
int power;
int height;
int loop(0);
const double a(70.5);
const double b(1);
const int NROWS(94);
const int NCOLS(50);
double court[NROWS][NCOLS];
int i, j;
//Read in values
lights >> option >> num_floodlights >> given_random >> measurement_1 >> measurement_2 >> measurement_3 >> measurement_4 >> power >> height;
//While loop that will run through the first two options of light placement
while(option != 'C')
{
for (i=0;i<NROWS;i++)
{
for (j=0;j<NCOLS;j++)
{
//Call the functions
distance = Distance(measurement_1, measurement_2);
intensity = Intensity(power, distance);
court[i][j]=intensity;
cout << setprecision(2) << "\t" << court[i][j];
}
}
//Read in next option
lights >> option >> num_floodlights >> given_random >> measurement_1 >> measurement_2 >> measurement_3 >> measurement_4 >> power >> height;
}
system("pause");
return 0;
}
Posting Freak
Posts: 1,359
Threads: 12
Joined: 2011-04
if you need further help, just ask a question(probably don't want to compile to see for errors). if its an error, post it. if you dont understand how to do something, post it as well.
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
Dudewitbow Wrote:if you need further help, just ask a question(probably don't want to compile to see for errors). if its an error, post it. if you dont understand how to do something, post it as well.
I guess I need help with my distance function.. as you can see from my code, it's only calculating the distance for the first floodlight. given measurement_1(which is 25 in the data file) and measurement_2(which is 23.5 in the data file). In the data file, the last piece of data, according to the project handout, is "Distance (feet) the lights are mounted above the positions". So I'm confused as to how I code my distance(D) function.
Posting Freak
Posts: 1,359
Threads: 12
Joined: 2011-04
InvictHero Wrote:I guess I need help with my distance function.. as you can see from my code, it's only calculating the distance for the first floodlight. given measurement_1(which is 25 in the data file) and measurement_2(which is 23.5 in the data file). In the data file, the last piece of data, according to the project handout, is "Distance (feet) the lights are mounted above the positions". So I'm confused as to how I code my distance(D) function.
after you declare the # of floodlights from the second digit, you have to have the floodlight data be an array of doubles(even if there is 1 floodlight e.g //double floodlightPower[numFloodlight] 
so when you pass in the array, your calculation array should have the ability in a forloop manner to calculate both distances by using a for loop inside of the distance function. if numFloodlight is 1, then the loop passes once. if numFloodlight is 2, then it should pass twice. i'll leave it to you to figure out how the array will find out how many times it has to loop.
Member
Posts: 146
Threads: 24
Joined: 2012-09
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: Alated
Server: Scania
Level: 200
Job: Kaiser
Guild: Hiatus
Guild Alliance: Light
Dudewitbow Wrote:after you declare the # of floodlights from the second digit, you have to have the floodlight data be an array of doubles(even if there is 1 floodlight e.g //double floodlightPower[numFloodlight]
so when you pass in the array, your calculation array should have the ability in a forloop manner to calculate both distances by using a for loop inside of the distance function. if numFloodlight is 1, then the loop passes once. if numFloodlight is 2, then it should pass twice. i'll leave it to you to figure out how the array will find out how many times it has to loop.
The only problem with this is that in the floodlightPower array, numFloodlight has to be a constant value :/ so idk what to do..
Posting Freak
Posts: 1,359
Threads: 12
Joined: 2011-04
totally slipped my mind. do you have access to any of the other data structures? (like locked mentioned, abilty to use vectors or deques?)
|