• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

C++ Help?

Status
Not open for further replies.

poweld

Member
cpp_is_king said:
Try deleting that \ at the end of the previous line. That's a line continuation marker in C. I'm sure a language lawyer can come in and say how the standard treats \ at the end of a comment line, but in any case I almost guarantee that's your problem, whether it's a problem that's backed up by the C standard or just a bug in your compiler.
Nice catch, I'm sure that's it.
 

Drkirby

Corporate Apologist
Alright, this seemed like it would be an easy enough problem to solve, but I am stumped, so I am just going to ask here.

I have an 2D array of characters (Actually a vector, but I don't think it matters). I want to output the array as a picture, to make it easy to view, where the image would be a 1:1 for the size of the array (An array of 100 x 100 would make a 100x100 image). Different characters are supposed to be different colors. Like A is a red, B is a blue, C is a Yellow, D is a brown, ect ect.

What would be the easiest way to do this? I don't need a specific image format ether, any should do.
 
Drkirby said:
Alright, this seemed like it would be an easy enough problem to solve, but I am stumped, so I am just going to ask here.

I have an 2D array of characters (Actually a vector, but I don't think it matters). I want to output the array as a picture, to make it easy to view, where the image would be a 1:1 for the size of the array (An array of 100 x 100 would make a 100x100 image). Different characters are supposed to be different colors. Like A is a red, B is a blue, C is a Yellow, D is a brown, ect ect.

What would be the easiest way to do this? I don't need a specific image format ether, any should do.

Really not as easy as it sounds. You'll need to access some GDI functions from Windows such as DrawText and MeasureText in order to render the letters in a font of your choosing onto a surface. Then, on top of that, you'll need to access image functions such as CreateBitmap etc so that you can save the results out to an image format.

Much easier would be to do this in C# if possible. The idea is the same, but the API is more friendly
 

Drkirby

Corporate Apologist
Did C++ have any sort of container I could use, that I could use a string as an index, or am I thinking of Java?
 

Kalnos

Banned
Also a note on that. Using a string as an index is usually not recommended in my experience, how about an enum?
 

Drkirby

Corporate Apologist
Kalnos said:
Also a note on that. Using a string as an index is usually not recommended in my experience, how about an enum?
Maybe. How about this, I'll explain what I am hoping to do, and see if there is a better way that I am missing.

I want to have an easily editable spreed sheet, so I could easily change the damage values units do in combat without having to recompile code (There is no simple formula, certain units simply do base X damage to anther unit, so I need a speed sheet like object). So, my plan was to have a Comma delimted spreed sheet (which is more or less just plan text), load it up into a 2D array, and have the row and column names be how I would reference a certain square, to look up the value.

Any way to convert a string to its corresponding Enum?
 

Kalnos

Banned
Question, are you searching the spreadsheet *just* to find the amount of damage/armor that a certain unit does or is there more depth than that? If it's the former then I would just use text files or XML for on-the-fly editing.

If you want an excel spread... and it seems like you're using this spreadsheet like a table in a database. I'm guessing it's something like:

Code:
UnitType ID  |  Damage   | Armor
1                 10         6
2                  6        12
3                  4         8
4                  8         4

If you did something like that then you could just assign a unit a 'type' and then pull in all damage/armor/etc values that correspond with their type. (Basically, if Type = number then read the entire row) Enum would work there because it would have a numeric value and a corresponding string name.
 

wolfmat

Confirmed Asshole
Drkirby said:
Maybe. How about this, I'll explain what I am hoping to do, and see if there is a better way that I am missing.

I want to have an easily editable spreed sheet, so I could easily change the damage values units do in combat without having to recompile code (There is no simple formula, certain units simply do base X damage to anther unit, so I need a speed sheet like object). So, my plan was to have a Comma delimted spreed sheet (which is more or less just plan text), load it up into a 2D array, and have the row and column names be how I would reference a certain square, to look up the value.
Couldn't you then just #define some named integer values and use those for indexed lookups in your arrays?

In pseudocode, something like this:
Code:
#define DMG_COL_FIREBALL 12
#define DMG_ROW_DRAGON 8
printf("Fireball damage of a dragon is %d", lookup_table[DMG_ROW_DRAGON][DMG_COL_FIREBALL]);
 

Drkirby

Corporate Apologist
Maybe an enum would work fine. I just wanted it to be easy to see what was both in the Spreed Sheet and what the units were, but I can just toss out the first column and row when taking info in

The Spreed Sheet is planed to look something like this.

Code:
X		Infintry	Tank	Boat	Plane Truck
Infintry	50		10	10	1	20
Tank		80		30	20	5	100
Boat		15		15	60	80	15
Plane		20		20	20	20	20
Truck		0		0	0	0	0

I can make use of that "blank" space to state just how many rows/columns there are, which would be useful.
 

Blizzard

Banned
Drkirby said:
Maybe an enum would work fine. I just wanted it to be easy to see what was both in the Spreed Sheet and what the units were, but I can just toss out the info part.

The Spreed Sheet is planed to look something like this.

Code:
X		Infintry	Tank	Boat	Plane Truck
Infintry	50		10	10	1	20
Tank		80		30	20	5	100
Boat		15		15	60	80	15
Plane		20		20	20	20	20
Truck		0		0	0	0	0

I think I can just toss out the column/row names though. In fact, I can make use of that "blank" space to state just how many rows/columns there are.
Are you going to be editing it from a GUI? Just use a 2D array, and index the rows and columns by something like UNIT_INFANTRY_INDEX, UNIT_TANK_INDEX, UNIT_BOAT_INDEX etc.

int someValue = damageTable[UNIT_INFANTRY_INDEX][UNIT_TANK_INDEX];

You could display them in a GUI as a bunch of edit boxes and use labels to display the row/column headings.
 

Drkirby

Corporate Apologist
I would really like to just edit it in excel.

This is what a Comma delimted spreadsheet looks like, if you never had to use one. More or less just plain text, but most spreadsheet editors can open it.

X,Boop,Stuff
Boop,5,35
Stuff,13,64
 

Blizzard

Banned
Drkirby said:
I would really like to just edit it in excel.

This is what a Comma delimted spreadsheet looks like, if you never had to use one. More or less just plain text, but most spreadsheet editors can open it.
So wait, do you basically want a config file that controls the settings, so you can edit it and relaunch your game?

Then yeah, you can still use the 2D array and enums to store it internally. You just have to write a bit of parsing logic (which will be simple since it's comma-separated and line-separated). If you need some pseudocode or if I misunderstood let me know.
 
Drkirby said:
Maybe. How about this, I'll explain what I am hoping to do, and see if there is a better way that I am missing.

I want to have an easily editable spreed sheet, so I could easily change the damage values units do in combat without having to recompile code (There is no simple formula, certain units simply do base X damage to anther unit, so I need a speed sheet like object). So, my plan was to have a Comma delimted spreed sheet (which is more or less just plan text), load it up into a 2D array, and have the row and column names be how I would reference a certain square, to look up the value.

Any way to convert a string to its corresponding Enum?

you could create a data structure that holds the name of a column as a string, and the values in that column as ints or whatever, loads all columns into a vector of that data structure, and have another vector of strings that holds only the names of the rows, then you can use the index of the name in the row vector to find the element you want in the column vector. put both structures into a class that holds them and a few functions for finding/returning elements

roughly like:

damage = columns[find_index_of_attacker(name)].get_damage_value(rows(find_index_of_defender(name)));

where find_index_of_xxx both take a string and return an index to an element in the vector that matches that string. you only need to store the actual information once so there's no point duplicating it for both column and row.

using enums would be faster but i'm thinking this way would be more human-readable for easily adding units. optimizing for speed would depend on how often you are calling the function.
 

wolfmat

Confirmed Asshole
Drkirby, you can of course ditch the empty first row, since the names of columns is identical to the names of rows, and shares its order. So all you need in the first CSV line is the first row with name first, then values.
Also, you don't need to say how many columns / rows there are.
Number of rows: Count CRLF in the CSV, add 1 (provided there's no CRLF at the end of the last line, which is commonly so for Excel iirc)
Number of columns: Cound commas in the first line, add 1
 
wolfmat said:
Drkirby, you can of course ditch the empty first row, since the names of columns is identical to the names of rows, and shares its order. So all you need in the first CSV line is the first row with name first, then values.
Also, you don't need to say how many columns / rows there are.
Number of rows: Count CRLF in the CSV, add 1 (provided there's no CRLF at the end of the last line, which is commonly so for Excel iirc)
Number of columns: Cound commas in the first line, add 1

oh yeah on that point he only needs 1 vector a data type holding string name and a container of the values for that column, then when it comes time to find the row just find the index of the vector of element with matching name and that's his row index, since the num columns and num rows will be identical if each unit appears once in row and column.
 

Drkirby

Corporate Apologist
Alright, I think I got that idea planed out for when I code it up, now for the current problem at hand.

I have a bunch of "cities", if they aren't next to each other, I want there to be a road linking them, and for all the cities to be linked up in a chain of roads.


So, lets say you have a 40x40 grid. In it, are an arbitrary amount of points, you have one maybe at (0,0), anther at (0,1), one at (39,39), one at (24,14), ect.

Does anyone have an idea of an algorithm I could use?

My current idea is to first parse the grid for all cities, then start doing a number of paces of the list to see if a city has anther city within i units. First check if there is one 1 space away, then go down the list and check for 2, then 3, ect. If it finds a pair (Or more), take them out of the list, and put them in some sort of objects and list to keep track of them, saying "Ok, put a road between these two". Then, I need to try and link up the segments of roads together. So, I will go down the list of roads find which two have the two shortest points to each other, and link them. Then, put it back into the list, and keep doing so till there is one one object.

Does this sound reasonable? I am not sure if it would even work right.
 

Blizzard

Banned
Drkirby said:
Alright, I think I got that idea planed out for when I code it up, now for the current problem at hand.

I have a bunch of "cities", if they aren't next to each other, I want there to be a road linking them, and for all the cities to be linked up in a chain of roads.


So, lets say you have a 40x40 grid. In it, are an arbitrary amount of points, you have one maybe at (0,0), anther at (0,1), one at (39,39), one at (24,14), ect.

Does anyone have an idea of an algorithm I could use?

My current idea is to first parse the grid for all cities, then start doing a number of paces of the list to see if a city has anther city within i units. First check if there is one 1 space away, then go down the list and check for 2, then 3, ect. If it finds a pair (Or more), take them out of the list, and put them in some sort of objects and list to keep track of them, saying "Ok, put a road between these two". Then, I need to try and link up the segments of roads together. So, I will go down the list of roads find which two have the two shortest points to each other, and link them. Then, put it back into the list, and keep doing so till there is one one object.

Does this sound reasonable? I am not sure if it would even work right.
It might be reasonable. I was initially thinking that doing repeated area searches could become incredibly expensive, but with only a few cities I think it would be okay.

Do you have any rules that you can think of for how you would like them linked? Or just "All cities must be connected to all other cities via roads", and the road can either be a long chain or a spidery network, whichever doesn't matter?
 

Drkirby

Corporate Apologist
Blizzard said:
It might be reasonable. I was initially thinking that doing repeated area searches could become incredibly expensive, but with only a few cities I think it would be okay.

Do you have any rules that you can think of for how you would like them linked? Or just "All cities must be connected to all other cities via roads", and the road can either be a long chain or a spidery network, whichever doesn't matter?
Lets say we have though some "large" number, like 150, for a really large map. Then what? I am somewhat hoping the map generation can scale easily.


rhfb said:

Thank you, after some TF2, I'll get it done. It has to be easier then reading though manuals on the BMP file format. This picture here:
image2.bmp


Took more time to make the part of the program to make the picture, then it did to make the part that produced the pictures image.

Actually, can anyone tell me why it will sometimes produce an image like this?


badimage.bmp


Code:
http://dl.dropbox.com/u/3496710/Maps/ConvertToBMP.cpp
http://dl.dropbox.com/u/3496710/Maps/ConvertToBMP.h
 

wolfmat

Confirmed Asshole
Drkirby said:
Alright, I think I got that idea planed out for when I code it up, now for the current problem at hand.

I have a bunch of "cities", if they aren't next to each other, I want there to be a road linking them, and for all the cities to be linked up in a chain of roads.


So, lets say you have a 40x40 grid. In it, are an arbitrary amount of points, you have one maybe at (0,0), anther at (0,1), one at (39,39), one at (24,14), ect.

Does anyone have an idea of an algorithm I could use?

My current idea is to first parse the grid for all cities, then start doing a number of paces of the list to see if a city has anther city within i units. First check if there is one 1 space away, then go down the list and check for 2, then 3, ect. If it finds a pair (Or more), take them out of the list, and put them in some sort of objects and list to keep track of them, saying "Ok, put a road between these two". Then, I need to try and link up the segments of roads together. So, I will go down the list of roads find which two have the two shortest points to each other, and link them. Then, put it back into the list, and keep doing so till there is one one object.

Does this sound reasonable? I am not sure if it would even work right.
It might be doable that way; I'd probably recommend computing vectors instead though.
The vector from A to B is B-A, that's your road; A and B are vectors representing city coordinates in your 2D plane.
The length (magnitude) of a 2D vector is sqrt( (x1-x2)^2 + (y1-y2)^2 ) — for comparisons, it's safe to ditch the sqrt though.
 

Blizzard

Banned
Drkirby said:
Thank you, after some TF2, I'll get it done. It has to be easier then reading though manuals on the BMP file format. This picture here:
image2.bmp


Took more time to make the part of the program to make the picture, then it did to make the part that produced the pictures image.
And that is one of the advantages of stuff like SDL and SFML, it should be like one line of code to save a BMP. :D
 

Kalnos

Banned
Notrollious said:
Gah my numbers return some weird string of numbers and characters. Everything compiles fine, but I can't figure out what's wrong.

How are you printing them out? Code?
 
Yeah. Have to write a simple program that will calculate perimeter, area, and the diagonal length of the parallelogram when the user enters a length and a width.

It compiles fine, no errors, and I can let it enter a length and width, but then when it outputs something almost hexadecimal.

Aan59.jpg


But I can't figure out what's wrong.
 

Kalnos

Banned
Yep... it's almost surely where you're actually printing the numbers with cout or printf... but I can't really tell without looking. I'm guessing you're printing the address in memory instead of the value.

EDIT: Hm, maybe not after seeing that image, just post your cout or printf line, or preferably all the relevant code.
 

Escape Goat

Member
Do we have any Visual Basic experts in here? Didn't really want to make a new thread since I figured some of you know a few languages.
 
Kalnos said:
Yep... it's almost surely where you're actually printing the numbers with cout or printf... but I can't really tell without looking. I'm guessing you're printing the address in memory instead of the value.

EDIT: Hm, maybe not after seeing that image, just post your cout or printf line, or preferably all the relevant code.


Code:
#include <iostream>
#include <cmath>
using namespace std;

//Perimeter function
double perimeter(double length, double width);

//Area function
double area(double length, double width);

//Diagonal length function
double diagonal(double length, double width);

//Main function
int main()
{
	double length, width;

	//Entering the length and width
	cout << "Enter the length: ";
	cin >> length;
	cout << "Enter the width: ";
	cin >> width;

	//Outputting the perimeter, area, and diagonal length
	cout << "The perimeter is: " << perimeter << " feet" << endl;
	cout << "The area is: " << area << " feet" << endl;
	cout << "The diagonal length is: " << diagonal << endl;
	
	
	return 0;
}
//Perimeter function
double perimeter( double length, double width)
{
	return (2*length + 2*width);
}

//Area function
double area (double length, double width)
{
	return (length * width);
}

//Diagonal function
double diagonal (double length, double width)
{
	return (sqrt(length*length + width * width));
}

I'm currently tinkering with it, so I know something's probably not right.
 

Kalnos

Banned
Teh Hamburglar said:
Do we have any Visual Basic experts in here? Didn't really want to make a new thread since I figured some of you know a few languages.

I use VB.NET at work, so I may be able to help you out.
 

Kalnos

Banned
I'm currently tinkering with it, so I know something's probably not right.

Pass variables to your functions, I.E... Function(Length, Width). You need to do this in the actual cout statement.

Code:
	cout << "The diagonal length is: " << diagonal(length, width) << endl;
 
Kalnos said:
Pass variables to your functions, I.E... Function(Length, Width). You need to do this in the actual cout statement.

Code:
	cout << "The diagonal length is: " << diagonal(length, width) << endl;


Gah. I knew it was something simple. My tired brain missed that. Thanks a bunch.
 

Chris R

Member
Teh Hamburglar said:
Do we have any Visual Basic experts in here? Didn't really want to make a new thread since I figured some of you know a few languages.
Vb or vb.net? Either way there is a regular programming help thread somewhere out there. Post there and I'm sure someone could help (I to use vb.net at work).
 
So I'm met my old C++ nemesis a few hours ago, Templates.

This is exercise 10-2 of Accelerated C++. The problem is to implement a median function, that works with both vectors and arrays of numeric types.

My first attempt was the following:

Code:
template <class Ran, class T>
T median (Ran b, Ran e)
{
	std::ptrdiff_t size = e - b;

	if (size == 0)
	{
		throw std::domain_error("median of an empty vector");
	}

	std::sort(b, e);

	std::size_t mid = size / 2;

	return size % 2 == 0 ? (b[mid] + b[mid - 1]) / 2 : b[mid];

	
}

Well, the compiler complains that it can't deduce the type of T. My assumption is that the last statement is doing both integer and floating point arithmetic and it doesn't know the type the result should be returned, int or double, etc.

A little later I tried something I saw in the accumulate algorithm of the standard library which does something similar to the following:

Code:
/*
 * Had to put a third argument similar to how accumulate works
 * because the compiler was complaining that it couldn't deduce
 * the type of T.
 *
 * To see this remove the third argument and return the expression
 * assigned to med.  If the container has integer elements the
 * arithmetic is performed using integer arithmetic but the / 2
 * might want to make the result in floating point so it gets confused
 * about which type to return.
 *
 * Or at least that's how I understand it.
 *
 */
template <class Ran, class T>
T median (Ran b, Ran e, T type)
{
	T med = type;

	std::ptrdiff_t size = e - b;

	if (size == 0)
	{
		throw std::domain_error("median of an empty vector");
	}

	std::sort(b, e);

	std::size_t mid = size / 2;

	med = size % 2 == 0 ? (b[mid] + b[mid - 1]) / 2 : b[mid];

	return med;

	
}

My questions are: Is my assumption about why the first solution doesn't work correct? Is there a way to implement the median without the 3rd parameter?
 

wolfmat

Confirmed Asshole
I'm not a templates guru, but I think the problem is that the type is ambiguous. Try casting to make sure only one type will match the return value.
 

maharg

idspispopd
You can do it that way, but you don't need to. Return types can't be determined by induction the way arguments are (the rvalue doesn't know the type of the lvalue its being assigned to, and there may be multiple lvalue types possible anyways).

You can just pass the return type argument as a template argument, though. Switch the template arguments around so the return type's template parameter is first and call it as median<return_type>(a, b); It will still deduce the second template parameter.
 

Drkirby

Corporate Apologist
Quick question, how do suppress warning in Visual Studio 2010?

These type bug me. I know that I am doing it, shut up:
warning C4244: 'initializing' : conversion from 'int' to 'float', possible loss of data

Yes yes, there is so much valuable data being lost converting 13 into 13.0. Clearly.
 

wolfmat

Confirmed Asshole
Unfortunately, the "shut the fuck up, I know what I'm doing" attitude is out of style since QuakeCon this year, so get crackin' :p
 

Drkirby

Corporate Apologist
wolfmat said:
Unfortunately, the "shut the fuck up, I know what I'm doing" attitude is out of style since QuakeCon this year, so get crackin' :p
Heh. I just fixed it all anyway, along with some signed/unsigned warnings I got in anther file.


Ok, how about this. I want to be able to pass in a seed by a launch argument, and have it default to being a random string if no seed is passed in. How would I go about doing it? Mainly want to do it to debug oddities in a random map generator, already made it so the seed is appended to the file name.
 

wolfmat

Confirmed Asshole
The main() method signature, decoded:
int main( int number_of_arguments, const char* arguments[] )

So if you wanted to print all arguments:
Code:
#include <stdio.h>
int main( int argc, const char* argv[] )
{
	for( int i = 0; i < argc; i++ )
	{
		printf( "Argument %d: %s\n", i, argv[i] );
	}
}
So if all you wanted would be the seed as the first parameter with a call like "game 13946132486", you'd find it in argv[1] (argv[0] is always the program name); you'd just have to convert it to your seed type (long, probably).
And obviously, if argv < 2, then you'd need the random seed.

For random numbers, I'd probably recommend Boost: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_random.html

Edit: If you wanted a flag, like --seed, followed by the seed, you'd scan for "--seed" in argv, then ensure that there's an argv in the next cell, and the next cell would contain your value in a call like "game --players 5 --inputs all --seed 198467124 --godmode 1"
 
Ok, so I ran into a random problem and it's vexing me, will you help, GAF?

Here is my code...

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

double file_average(ifstream& fin);
double file_deviation(ifstream& fin, double average);

int main()
{
ifstream fin;
cout << "Begin averaging file...\n";
fin.open("midtermData.txt");
if( fin.fail())
{
cout << "Input File open fail.\n";
exit(1);
}

file_average(fin);
fin.close();

cout << "Begin determining deviation of file...\n";
fin.open("midtermData.txt");
if( fin.fail())
{
cout << "Input File open fail.\n";
exit(1);
}

file_deviation(fin, file_average(fin));
fin.close();

return 0;
}

double file_average(ifstream& fin)
{
double numbers[18];
double sum;
double average;

while (!fin.eof())
{
int i = 0;
fin >> numbers;
sum += numbers;
i++;
cout << numbers;
}
average = sum/18;
cout << average << endl;


return average;
}

double file_deviation(ifstream& fin, double average)
{
double numbers[18];
double dataPoints[18];
double deviation;
double numToBeSquared;
double sqNum;


while (!fin.eof())
{
int i = 0;
fin >> numbers;
numbers;
i++;
}

for (int i =0; i <=18; i++)
{
dataPoints = sqrt((numbers)-(average));
// cout << numbers << endl;
}


cout << deviation << endl;
}



It's supposed to determine the standard deviation from a .txt file that looks like this...

456.34 645.36 752.37 911.49 134.32 614.38 736.43 914.72 369.41
558.33 427.46 511.96 889.98 472.54 544.45 852.35 664.72 489.43

I wrote the file_average func earlier today and it worked great. Now it's giving some strange numbers and I don't understand why. The actual formula hasn't been finished yet,since when I run everything together I get NaN from the program. The array that holds the data read from the file worked earlier, and now it doesn't and I don't know why. Any help would be appreciated, thanks.
 
Read the numbers into an array first, then forget about the file and only operate on the array.

Youre reading the file 3 times here. The first time you read the file (the first time you call file_average, you totally throw away the results. In other words, you call the function, never use the results, then you do the same thing again.

The real problem is the second time, when you write

File_deviation(fin, file_average(fin))

After that file_average, youre at the end of the file and file_deviation has nothing to read.

So do yourself a favor. Write a function like

void load_numbers(ifstream& fin, double* numbers)

and then never look at the file again
 
Thanks for the help. I keep reading garbage values from the file and I don't understand why. if I print the numbers array to the screen I get crazy results, but the average still gets computed correctly. I'm going to look into setting width, I suppose.
 
Shaundakahl said:
Thanks for the help. I keep reading garbage values from the file and I don't understand why. if I print the numbers array to the screen I get crazy results, but the average still gets computed correctly. I'm going to look into setting width, I suppose.

First things first: stop reading the file so many times.

After that, you can fix the problem with the numbers printing wrong. (btw, its because youre printing the number after incrementing i, but still, fix the other problem first)
 

Exuro

Member
Okay I need some help with this code. I'm still a beginner and am having trouble with checking values that are greater or less than. The code is spliced/redone from another code I'm working on so its fully "functioning" in that it runs, but for some reason I'm not getting what I want. I threw in a lot of cout stuff so its easier for me to check numbers and such.

Code:
#include<iostream>
#include<cmath>
using namespace std;
main()
{
int i, x;
int size = 10;
int numBin;
double bWidth;
double array[10];
int arrayBin[4];
double min = 0;
double max = 10.1;
double temp;


array[0] = 0;
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 2;
array[5] = 10.1;
array[6] = 10.1;
array[7] = 10.1;
array[8] = 10.1;
array[9] = 10.1;

bWidth =  abs((max-min)/size);

temp = (max-min)/bWidth;
temp = temp + .5;
numBin = temp;

int numMax = 0;
for (x = 0; x < size; x++)
{ 
	if (max <= array[x])
	numMax++;
	cout << numMax << endl;
	cout << max << endl;
}
cout << "numMax is " << numMax << endl;
cout << "bWidth is " << bWidth << endl;
cout << "numBin is " << numBin << endl;
cout << "min is " << min << endl;
cout << "max is " << max << endl;

for (i = 0; i < numBin; i++)
	arrayBin[i] = 0;

for (x = 0; x < size; x++)
{
	cout << "x = " << x << endl;
	for (i = 0; i < numBin; i++)
	{
		cout << "i = " << i << endl;
		if (array[x] >= min + bWidth*i && array[x] < min + bWidth*(i+1))
			{
			cout << "array element " << x << " with value "<< array[x] << " is less than " << min + bWidth*(i+1) << endl;
			arrayBin[i]++;
			
			}
	}
}

//arrayBin[9] = arrayBin[9] + numMax;

for (i = 0; i < numBin; i++)
	cout << "Bin number#"<< i << " " << arrayBin[i] << endl;

}

It's for a histogram, determining number of bins and bin sizes and then counting how many numbers were placed in each bin. My issue is when I have a maximum value/s of a decimal number like 10.1 my little less than or greater than function here doesn't work.

Code:
if (array[x] >= min + bWidth*i && array[x] < min + bWidth*(i+1))
			{
			cout << "array element " << x << " with value "<< array[x] << " is less than " << min + bWidth*(i+1) << endl;
			arrayBin[i]++;

It says that 10.1 is less than 10.1 which it isn't. If I put 10 in then it skips it properly. Hard to explain but if you run it and look over the code hopefully you understand what I'm trying to do. I have some commented code about numMax near the end which will take the maximum value/s and put them in the last bin but those decimal max numbers aren't getting skipped for some reason.
 

Exuro

Member
Ugh this is making the program more complicated than I hoped. Gonna read over the link. Is there any other way I could make my bins and bin size ect. without having to deal with the 10.1 > 10.1 thing or is that the only way?
 
Status
Not open for further replies.
Top Bottom