I have an array of a Struct and trying to sort them alphabetically by lname ,i tried bubble sort but when we have some empty items in our array bubble sort won't work correctly .so is there any function to sort these items ?
This is my code :
#include<iostream>
#include<algorithm>
using namespace std;
struct user {
char lname[30];
int userid;
};
user libuser[1000];
int main(){
strcpy(libuser[0].lname,"");
libuser[0].userid = 0;
strcpy(libuser[1].lname,"backzade");
libuser[1].userid = 1;
strcpy(libuser[2].lname,"akhondali");
libuser[2].userid = 2;
strcpy(libuser[3].lname,"sayidian");
libuser[3].userid = 3;
strcpy(libuser[4].lname,"navah");
libuser[4].userid = 4;
strcpy(libuser[5].lname,"mostarab");
libuser[5].userid = 5;
libuser[6].userid = 0;
strcpy(libuser[7].lname,"");
libuser[7].userid = 0;
strcpy(libuser[8].lname,"");
libuser[8].userid = 0;
strcpy(libuser[9].lname,"borobaba");
libuser[9].userid = 9;
strcpy(libuser[10].lname,"divune");
libuser[10].userid = 10;
for(int i=1;i<1000;i++)
if(libuser[i].userid!=0)
cout<<libuser[i].lname<<"\n";
system("PAUSE");
return 0;
}
This code just can be one method among many good methods. I just use std::sort and lambda.
I hope this will help you a little. Happy new year!
std::sort (std::begin(libuser), std::end(libuser), [&](const user& first, const user& second) -> bool
{
return (first.lname[0] < second.lname[0]);
});
#include <iostream>
#include <algorithm>
using namespace std;
struct user
{
char lname[30];
int userid;
};
user libuser[1000];
int elementCount = 0;
int main()
{
strcpy(libuser[0].lname,"");
libuser[0].userid = 0;
strcpy(libuser[1].lname,"backzade");
libuser[1].userid = 1;
strcpy(libuser[2].lname,"akhondali");
libuser[2].userid = 2;
strcpy(libuser[3].lname,"sayidian");
libuser[3].userid = 3;
strcpy(libuser[4].lname,"navah");
libuser[4].userid = 4;
strcpy(libuser[5].lname,"mostarab");
libuser[5].userid = 5;
libuser[6].userid = 0;
strcpy(libuser[7].lname,"");
libuser[7].userid = 0;
strcpy(libuser[8].lname,"");
libuser[8].userid = 0;
strcpy(libuser[9].lname,"borobaba");
libuser[9].userid = 9;
strcpy(libuser[10].lname,"divune");
libuser[10].userid = 10;
//sort
for(int i = 0; i < 1000; i++)
{
for(int j = 0; j < 1000 - 1; j++)
{
if(strcmp(libuser[j].lname, libuser[j + 1].lname) > 0) //change to < 0 for descending sort
{
user temp = libuser[j];
libuser[j] = libuser[j + 1];
libuser[j + 1] = temp;
}
}
}
for(int i = 1; i < 1000; i++)
{
if(libuser[i].userid!=0)
{
cout<<libuser[i].lname<<"\n";
}
}
system("PAUSE");
return 0;
}
Related
I have the following function and the following struct.
void function(TestStruct *array) {
for (int i = 0; i < 3; i++){
array[i].value = 0;
array[i].remove = true;
}
}
struct TestStruct
{
int value;
bool remove;
};
int main(){
TestStruct sampleStruct[3];
sampleStruct[0].value = 1;
sampleStruct[1].value = 2;
sampleStruct[2].value = 3;
sampleStruct[0].value = false;
sampleStruct[1].value = false;
sampleStruct[2].value = false;
function(sampleStruct);
}
However, the values in sampleStruct do not change.
When I was debugging, the function had only access to the first index of the array 0x0025fb64 {Value = 1 remove = false}, but even the first index did not change.
In other words, I'm trying to access the full sampleStruct address to change the values.
Well, for me it works fine. The only thing I changed is .value = false to .remove = false (because this is probably how you wanted to do it). code:
#include <iostream>
using namespace std;
struct TestStruct
{
int value;
bool remove;
};
void function(TestStruct *array) {
for (int i = 0; i < 3; i++){
array[i].value = 0;
array[i].remove = true;
}
}
int main(){
TestStruct sampleStruct[3];
sampleStruct[0].value = 1;
sampleStruct[1].value = 2;
sampleStruct[2].value = 3;
sampleStruct[0].remove = false; // changed to .remove
sampleStruct[1].remove = false; // changed to .remove
sampleStruct[2].remove = false; // changed to .remove
function(sampleStruct);
for (int i = 0; i < 3; i++) // prints "0 1" 3 times
cout << sampleStruct[i].value << ' ' << sampleStruct[i].remove << '\n';
}
I am having an issue with accessing 2d array elements.
While creating an object I allocate memory for my array in the constructor.
Once the object is created when I try to access array elements I am getting EXC_BAD_ACCESS.
I checked and when still in constructor I can access array elements.
I don’t know what I am doing wrong.
This is my class where I allocate memory for data array;
class TableData
{
public:
TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), columnCount(columns)
{
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
columnID.resize(columnCount);
//Below I did a test where can see that I can access array elements
data[0][0]=“test1111”;
std::string test = data[0][0];
}
~TableData()
{
for (int count = 0; count < rowCount; ++count)
delete[] data[count];
delete[] data;
}
std::string **data;
std::string tableName = "";
const int rowCount;
const int columnCount;
std::vector<std::string> columnID;
};
When I try to fill an array with data I am getting "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)"
ecuData.tables.push_back(TableData(name, tableRowCount, tableColumnCount));
ecuData.tables[i].data[0][0]=“test2222”; // Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
“tables” is a vector of TableData objects
Here is a full function:
void Decoder::getTables(std::fstream &fs, std::vector<char> & buffer, SGBDdata &ecuData)
{
const int ptr_offset = 0x84;
int tableOffset = *reinterpret_cast<int32_t*>(&buffer[0] + ptr_offset);
const int tableCountBufferLength = 4;
vector<char> tableCountBuffer;
fs.seekg(tableOffset, fs.beg);
readAndDecryptBytes(fs, tableCountBuffer, tableCountBufferLength);
int tableCount = *reinterpret_cast<int32_t*>(&tableCountBuffer[0] + 0);
for(int i = 0; i < tableCount; ++i) //iterate through tables
{
int tableBufferLength = 0x50;
vector<char> tableBuffer;
readAndDecryptBytes(fs, tableBuffer, tableBufferLength);
string name(tableBuffer.begin(), tableBuffer.begin() + 0x40);
TrimEnd(name);
int tableColumnOffset = *reinterpret_cast<int32_t*>(&tableBuffer[0] + 0x40);
int tableColumnCount = *reinterpret_cast<int32_t*>(&tableBuffer[0] + 0x48);
int tableRowCount = *reinterpret_cast<int32_t*>(&tableBuffer[0] + 0x4C);
//**This is where I am creating new “TableData” object and putting it into vector<TableData>**
ecuData.tables.push_back(TableData(name, tableRowCount, tableColumnCount));
long savedPos = fs.tellg();
fs.seekg(tableColumnOffset, fs.beg);
//Load column names
for(int j = 0; j < tableColumnCount; ++j)
{
int tableItemBufferLength = 1024;
vector<char> tableItemBuffer;
for(int k = 0; k < tableItemBufferLength; ++k)
{
readAndDecryptBytes(fs, tableItemBuffer, 1);
if (tableItemBuffer[k] == 0)
break;
}
ecuData.tables[i].columnID.push_back(string(tableItemBuffer.begin(), tableItemBuffer.end() - 1));
}
for(int j = 0; j < tableRowCount; ++j)
{
for (int k = 0; k < tableColumnCount; ++k)
{
int tableItemBufferLength = 1024;
vector<char> tableItemBuffer;
for (int l = 0; l < tableItemBufferLength; ++l)
{
readAndDecryptBytes(fs, tableItemBuffer, 1);
if (tableItemBuffer[l] == 0)
break;
}
string s(tableItemBuffer.begin(), tableItemBuffer.end() - 1);
ecuData.tables[i].data[j][k] = s; //**This is where I get my fault**
}
}
fs.seekg(savedPos, fs.beg);
}
}
Here is SGBDdata class. The ecuData object of this class contains a vector data
class SGBDdata
{
public:
std::string sgbdPath;
std::string ecuName;
std::vector<JobData> jobs;
std::vector<TableData> tables;
};
lose all the new, delete, and pointer magic. and use a vector of vectors?
// Example program
#include <iostream>
#include <string>
#include <vector>
class TableData
{
public:
TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), columnCount(columns)
{
data.resize(columnCount);
for( int i = 0; i < columnCount; i++)
{
data[i].resize(rowCount);
}
columnID.resize(columnCount);
//Below I did a test where can see that I can access array elements
data[0][0]="test1111";
std::string test = data[0][0];
}
~TableData()
{
}
std::vector<std::vector<std::string>> data;
std::string tableName = "";
const int rowCount;
const int columnCount;
std::vector<std::string> columnID;
};
int main()
{
TableData test = TableData( "me", 3,3);
test.data[0][0]="test2222";
std::cout<<test.data[0][0];
}
I have managed to get the code working. As #dratenik mentioned the problem was related to shallow copy which was taking place in default copy constructor. To get it fixed I have added a move constructor to TableData class.
class TableData
{
public:
TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), columnCount(columns)
{
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
columnID.resize(columnCount);
}
TableData(TableData&& t) : rowCount(t.rowCount), columnCount(t.columnCount)
{
tableName=t.tableName;
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
for (int r_count = 0; r_count < rowCount; ++r_count)
for (int c_count = 0; c_count < columnCount; ++c_count)
data[r_count][c_count] = t.data[r_count][c_count];
}
~TableData()
{
for (int count = 0; count < rowCount; ++count)
delete[] data[count];
delete[] data;
}
std::string **data;
std::string tableName = "";
const int rowCount;
const int columnCount;
std::vector<std::string> columnID;
};
Having a problem in making a dynamic box using these set of ASCII codes. I'm really lost on what to do. This dynamic box also has numbers inside them.
I already tried making loops inside loops to adjust the box's size.
int main(){
char asciis[] = {'\xDA','\xB3','\xC3','\xC0','\x20','\xC4','\xC5','\xC1','\xBF','\xB4','\xD9', '\xC2'};
int box size = (n*2)+1;
char box[box size][box size]; //set the size of the box
//set a in all coordinates
/*for (int r = 0; r < n; r++){
for (int c = 0; c < n; c++){
box[r][c] = 'a';
}
}*/
for (int r = 0; r < n; r++){
for (int c = 0; c < n; c++){
for (int boxrow = 0; boxrow < box size; boxrow++){
for (int boxcol = 0; boxcol < box size; boxcol++){
//conditions
}
}
}
cout << endl;
}
}
This is the output I'm trying to create:
https://i.imgur.com/YRgMlaJ.png
Don't mind those numbers, I was just mapping the array.
I'm sure there's a simpler solution, but off the top of my head:
#include <iostream>
using namespace std;
enum BoxParts {
kLeftUpper = '\xDA',
kVertical = '\xB3',
kLeftBreak = '\xC3',
kLeftLower = '\xC0',
kEmpty = '\x20',
kHorizontal = '\xC4',
kIntersection = '\xC5',
kBottomBreak = '\xC1',
kRightUpper = '\xBF',
kRightBreak = '\xB4',
kRightLower = '\xD9',
kTopBreak = '\xC2',
kCount = 12
};
void drawLine(const int cellCount, const int cellWidth, const char left, const char divider, const char contents, const char right)
{
cout << left;
for (int i = 1; i < cellCount * cellWidth; ++i)
{
if (0 == i % cellWidth)
cout << divider;
else
cout << contents;
}
cout << right << endl;
}
void drawBox(const int cellCount, const int cellWidth, const int cellHeight)
{
// top
drawLine(cellCount, cellWidth, kLeftUpper, kTopBreak, kHorizontal, kRightUpper);
for (int i = 1; i < cellCount * cellHeight; ++i)
{
if (0 == i % cellHeight)
drawLine(cellCount, cellWidth, kLeftBreak, kIntersection, kHorizontal, kRightBreak);
else
drawLine(cellCount, cellWidth, kVertical, kVertical, ' ', kVertical);
}
// bottom
drawLine(cellCount, cellWidth, kLeftLower, kBottomBreak, kHorizontal, kRightLower);
}
int main(int argc, char** argv) {
const int n = 4;
drawBox(n, 10, 5);
getchar();
return 0;
}
Produces:
I am trying to use the suggestion from this post to free up time being spent in _platform_memmove$VARIANT$Haswell. According to a time profiler, this is occurring when I send a pointer to several class instances to a function. I have tried changing the way I declare the class instances, changing what the function takes, etc. but have not been able to resolve this.
The chunk of my code that may help:
Inputs *tables = new Inputs(OutputFolder, DataFolder);
ScreenStrat *strat_burnin = new ScreenStrat(ScreenStrat::NoScreen, ScreenStrat::NoScreen,
tables->ScreenStartAge, tables->ScreenStopAgeHIV,
tables->ScreenStopAge, ScreenStrat::NoVaccine);
calibrate *calib_output = new calibrate ();
StateMachine *Machine = new StateMachine();
for (int i = 0; i < n_sims; i++){
calib_output->saved_output[i] = RunCalibration(calib_output->calib_params[i], *strat_burnin, *tables, *Machine);
}
auto ret_val = *calib_output;
delete strat_burnin;
delete tables;
delete Machine;
delete calib_output;
return(ret_val);
and then the function declaration:
vector<double> RunCalibration(vector<double> calib_params, ScreenStrat &strat_burnin, Inputs &tables, StateMachine &Machine)
EDIT
I addressed the points #Botje suggest and it hasn't fixed the problems. Updated code:
void RunCalibration(calibrate &calib, ScreenStrat &strat_burnin, Inputs &tables, StateMachine &Machine, int i);
unique_ptr<calibrate> RunChain(string RunsFileName, string CurKey, string OutputFolder, string DataFolder);
int main(int argc, char* argv[]) {
string DataFolder;
string OutputFolder;
DataFolder = "../Data/";
OutputFolder = "../Output/";
unsigned int run;
string CurKey;
string RunsFileName(DataFolder);
if(argc == 1){
RunsFileName.append("test.ini");
}
else if(argc > 1){
RunsFileName.append(argv[1]);
}
CIniFile RunsFile(RunsFileName);
if (!RunsFile.ReadFile()) {
cout << "Could not read Runs File: " << RunsFileName << endl;
exit(1);
}
CurKey = RunsFile.GetKeyName (0);
if (RunsFile.GetValue(CurKey, "RunType") == "Calibration"){
int totaliters = RunsFile.GetValueI(CurKey, "Iterations");
int n_sims = RunsFile.GetValueI(CurKey, "Simulations");
vector<future<unique_ptr<calibrate>>> futures;
vector<unique_ptr<calibrate>> modeloutputs;
for (run = 0; run < totaliters; run++){
futures.push_back (async(launch::async, RunChain, RunsFileName, CurKey, OutputFolder, DataFolder));
}
for (int i = 0; i < futures.size(); i++){
modeloutputs.push_back (futures[i].get());
} return(0)}
unique_ptr<calibrate> RunChain(string RunsFileName, string CurKey, string OutputFolder, string DataFolder) {
Inputs *tables = new Inputs(OutputFolder, DataFolder);
tables->loadRFG (RunsFileName, CurKey);
tables->loadVariables ();
int n_sims = tables->Simulations;
int n_params = tables->Multipliers.size();
int n_targs = tables->CalibTargs.size();
ScreenStrat *strat_burnin = new ScreenStrat(ScreenStrat::NoScreen, ScreenStrat::NoScreen,
tables->ScreenStartAge, tables->ScreenStopAgeHIV,
tables->ScreenStopAge, ScreenStrat::NoVaccine);
calibrate *calib_output = new calibrate (n_sims, n_params, n_targs);
calib_output->multipliers_names = tables->MultipliersNames;
calib_output->calib_targs_names = tables->CalibTargsNames;
for (int i = 0; i < n_targs; i ++){
calib_output->calib_targs[i] = tables->CalibTargs[i][0];
calib_output->calib_targs_SD[i] = tables->CalibTargs[i][1];
}
for (int i = 0; i < n_params; i++){
for (int j = 0; j < 3; j++){
calib_output->multipliers[i][j] = tables->Multipliers[i][j];
}
}
StateMachine *Machine = new StateMachine();
for (int i = 0; i < n_sims; i++){
RunCalibration(*calib_output, *strat_burnin, *tables, *Machine, i);
}
unique_ptr<calibrate> ret_val = make_unique<calibrate>(*calib_output);
delete strat_burnin;
delete tables;
delete Machine;
delete calib_output;
return(ret_val);
}
void RunCalibration(calibrate &calib, ScreenStrat &strat_burnin, Inputs &tables, StateMachine &Machine, int i){
Adding in Calibrate definition per request from #botje
#include "calibrate.h"
using namespace std;
calibrate::calibrate(int n_sims, int n_params, int n_targs) {
calib_targs.resize (n_targs);
calib_targs_SD.resize (n_targs);
multipliers.resize(n_params);
for(int i = 0; i < n_params; i++){
multipliers[i].resize(3);
}
calib_params.resize (n_sims);
for (int i = 0; i < calib_params.size(); i++){
calib_params[i].resize (n_params);
}
saved_output.resize (n_sims);
for (int i = 0; i < saved_output.size(); i++){
saved_output[i].resize (n_targs);
}
best_params.resize (n_params);
GOF.clear();
tuned_SD.resize(n_params);
}
calibrate::~calibrate(void) {
}
void calibrate::CalculateGOF(int n_sims) {
GOF.push_back (WeightedDistance (saved_output[n_sims][0], calib_targs[0], calib_targs_SD[0]));
for (int i = 1; i < calib_targs.size(); i ++){
GOF[n_sims] += WeightedDistance (saved_output[n_sims][i], calib_targs[i], calib_targs_SD[i]);
}
if (n_sims == 0){
GOF_min = GOF[0];
best_params = calib_params[0];
} else {
auto it = std::min_element(std::begin(GOF), std::end(GOF));
int index = distance(GOF.begin(), it);
GOF_min_run = GOF[index];
if (GOF_min_run < GOF_min){
GOF_min = GOF_min_run;
best_params = calib_params[index];
}
}
}
std::vector<double> calibrate::loadCalibData(int n_params, int n_sim, int tuning_factor) {
if(n_sim == 0){
random_device rd;
mt19937 gen(rd());
for (int i = 0; i < n_params; i ++ ){
uniform_real_distribution<> dis(multipliers[i][0], multipliers[i][1]);
calib_params[n_sim][i] = dis(gen);
}
} else {
tuned_SD = tuningparam (n_sim, n_params, tuning_factor);
for (int i = 0; i < n_params; i ++ ){
calib_params[n_sim][i] = rnormal_trunc (best_params[i], tuned_SD[i], multipliers[i][1], multipliers[i][0]);
}
}
return(calib_params[n_sim]);
}
double calibrate::WeightedDistance(double data, double mean, double SD) {
double distance = pow((data - mean)/(SD * 2),2);
return distance;
}
double calibrate::rnormal_trunc(double mu, double sigma, double upper, double lower) {
std::default_random_engine generator;
std::normal_distribution<double> distribution(mu, sigma);
double prob = distribution(generator);
while (prob < lower || prob > upper){
prob = distribution(generator);
}
return(prob);
}
vector<double> calibrate::tuningparam(int n_sims, int n_param, int tuning_factor) {
vector<double> newSD;
for (int i = 0; i < n_param; i++){
newSD.push_back (multipliers[i][2]/pow(tuning_factor,n_sims));
}
return newSD;
}
I improved RunCalibration as follows. Note the comments for further improvement opportunities.
using std::make_unique;
using std::unique_ptr;
void RunCalibration(calibrate &calib, ScreenStrat &strat_burnin, Inputs &tables, StateMachine &Machine, int i);
unique_ptr<calibrate> RunChain(string RunsFileName, string CurKey, string OutputFolder, string DataFolder) {
auto tables = make_unique<Inputs>(OutputFolder, DataFolder);
tables->loadRFG (RunsFileName, CurKey);
tables->loadVariables ();
int n_sims = tables->Simulations;
int n_params = tables->Multipliers.size();
int n_targs = tables->CalibTargs.size();
auto strat_burnin = make_unique<ScreenStrat>(
ScreenStrat::NoScreen, ScreenStrat::NoScreen,
tables->ScreenStartAge, tables->ScreenStopAgeHIV,
tables->ScreenStopAge, ScreenStrat::NoVaccine);
auto calib_output = make_unique<calibrate>(n_sims, n_params, n_targs);
// I don't know the type of these fields, but IF you do not modify them in
// `RunCalibration`, consider making them `shared_ptr<vector<...>>`
// both in `calibrate` and in `Inputs` so you can simply copy
// the pointer instead of the full table.
calib_output->multipliers_names = tables->MultipliersNames;
calib_output->calib_targs_names = tables->CalibTargsNames;
// Same applies here. If you do not modify CalibTargs, make `calib_targs` a shared_ptr
// and only copy by pointer.
for (int i = 0; i < n_targs; i ++){
calib_output->calib_targs[i] = tables->CalibTargs[i][0];
calib_output->calib_targs_SD[i] = tables->CalibTargs[i][1];
}
// and again...
for (int i = 0; i < n_params; i++){
for (int j = 0; j < 3; j++){
calib_output->multipliers[i][j] = tables->Multipliers[i][j];
}
}
auto Machine = make_unique<StateMachine>();
for (int i = 0; i < n_sims; i++){
RunCalibration(*calib_output, *strat_burnin, *tables, *Machine, i);
}
// This will return the unique_ptr without copying.
return calib_output;
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int arraylength;
int lastbig = 0;
int lastsmall = 0;
int temp = 0;
int numofgroups = 0;
double gg = 0;
cout<<"Enter the number of numbers you are going to enter "<<endl;
cin>>arraylength;
int data[arraylength];
for(int ahmet = 0;ahmet < arraylength;ahmet++)
{
cout<<"Enter the num no."<<ahmet+1<<endl;
cin>>data[ahmet];
}
for(int bbm = 0;bbm < arraylength;bbm++)
{
if(data[bbm]>lastbig)
{
lastbig = data[bbm];
}
}
cout<<"Biggest "<<lastbig<<endl;
for(int ddr = 0;ddr < arraylength;ddr++)
{
if(data[ddr]<lastbig && lastsmall == 0)
{
lastsmall = data[ddr];
}
else if(data[ddr]<lastsmall)
{
lastsmall = data[ddr];
}
}
cout<<"smallest "<<lastsmall<<endl;
temp = lastbig-lastsmall;
cout<<"Enter the number of groups you want"<<endl;
cin>>numofgroups;
gg = (double)temp/numofgroups;
cout<<"gg ="<<gg;
gg = ceil(gg);
cout<<"gg ="<<gg<<endl;
int z = 0;
int lastnumleft = 0;
struct groups {
int min;
int max;
int membercount;
}group[numofgroups];
int tmp = lastsmall;
for(int dinghy = 0;dinghy<numofgroups;dinghy++)
{
if(dinghy == 0)
{
group[dinghy].min = tmp;
group[dinghy].max = tmp + ((int)gg - 1);
tmp = tmp + (int)gg;
}
else{
group[dinghy].min = tmp;
group[dinghy].max = tmp+((int)gg-1);
tmp = tmp + (int)gg;
}
}
for(int jpn = 0;jpn<numofgroups;jpn++)
{
for(int mtr = 0;mtr<arraylength;mtr++)
{
if(data[mtr]>group[jpn].min&&data[mtr]<group[jpn].max)
{
group[jpn].membercount++;
}
}
}
for(int dingil = 0;dingil<numofgroups;dingil++)
{
if(!group[dingil].membercount){
group[dingil].membercount = 0;
}
}
for(int xyz = 0;xyz<numofgroups;xyz++)
{
cout<<group[xyz].min<<" - "<<group[xyz].max<<" "<<group[xyz].membercount<<endl;
}
cin.ignore();
cin.get();
return 0;
}
This program actually does the calculations needed for making a histogram member count of groups and min max of numbers but i cant group the numbers can you help me :)
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int arraylength;
int lastbig = 0;
int lastsmall = 0;
int temp = 0;
int numofgroups = 0;
double gg = 0;
cout<<"Enter the number of numbers you are going to enter "<<endl;
cin>>arraylength;
int *data = new int[arraylength];
for(int ahmet = 0;ahmet < arraylength;ahmet++)
{
cout<<"Enter the num no."<<ahmet+1<<endl;
cin>>data[ahmet];
}
for(int bbm = 0;bbm < arraylength;bbm++)
{
if(data[bbm]>lastbig)
{
lastbig = data[bbm];
}
}
cout<<"Biggest "<<lastbig<<endl;
for(int ddr = 0;ddr < arraylength;ddr++)
{
if(data[ddr]<lastbig && lastsmall == 0)
{
lastsmall = data[ddr];
}
else if(data[ddr]<lastsmall)
{
lastsmall = data[ddr];
}
}
cout<<"smallest "<<lastsmall<<endl;
temp = lastbig-lastsmall;
cout<<"Enter the number of groups you want"<<endl;
cin>>numofgroups;
gg = (double)temp/numofgroups;
cout<<"gg ="<<gg;
gg = ceil(gg);
cout<<"gg ="<<gg<<endl;
int z = 0;
int lastnumleft = 0;
struct groups {
int min;
int max;
int membercount;
}*group;
group = new groups[numofgroups];
int tmp = lastsmall;
for(int dinghy = 0;dinghy<numofgroups;dinghy++)
{
if(dinghy == 0)
{
group[dinghy].min = tmp;
group[dinghy].max = tmp + ((int)gg - 1);
tmp = tmp + (int)gg;
}
else
{
group[dinghy].min = tmp;
group[dinghy].max = tmp+((int)gg-1);
tmp = tmp + (int)gg;
}
}
for(int jpn = 0;jpn<numofgroups;jpn++)
{
//need to initialize as it has some garbage value and that is what it is printing out.
group[jpn].membercount = 0;
for(int mtr = 0;mtr<arraylength;mtr++)
{
// note the equalities as you need to include the first and the last numbers
if(data[mtr]>=group[jpn].min&&data[mtr]<=group[jpn].max)
{
group[jpn].membercount++;
}
}
}
for(int dingil = 0;dingil<numofgroups;dingil++)
{
if(!group[dingil].membercount){
group[dingil].membercount = 0;
}
}
for(int xyz = 0;xyz<numofgroups;xyz++)
{
cout<<group[xyz].min<<" - "<<group[xyz].max<<" "<<group[xyz].membercount<<endl;
}
cin.ignore();
cin.get();
return 0;
}
It'll work fine now :)