Try dividing by 10 until the number as an int equals 0.
float temp = net_pay[i];
while ( (int)temp > 0 ) {
temp /= 10;
}
printf( "Before the radix -- %f\n", (float)(int)net_pay[i] );
printf( "After the radix -- %f\n", temp );
Try dividing by 10 until the number as an int equals 0.
float temp = net_pay[i];
while ( (int)temp > 0 ) {
temp /= 10;
}
printf( "Before the radix -- %f\n", (float)(int)net_pay[i] );
printf( "After the radix -- %f\n", temp );
State means that info persists between calls. If you want to keep a running total of some statistic, that's state, and it's hard to maintain with functions. You have to use a global variable or a parameter.
int count1 = 0;
void function( int& count2 ) {
// do work
++count1;
++count2;
}
But global variables are always bad, and with callbacks the parameter is harder to work with because you have to pass it along whenever you use the callback.
void callback( int& count ) {
// do work
++count;
}
void function( void (*cb)( int& ), int& count ) {
cb( count );
}
Add a few more functions in the callback chain and a few more state parameters and it gets really messy. Functors make the problem go away because they're objects that can have inside information.
struct functor {
int count;
functor(): count( 0 ) {}
void operator()() {
// do work
++count;
}
};
void function( functor& f ) {
f();
}
Add as many functions to the callback chain and as many state variables as you want and it stays simple. :) That's why functors are part of the facade design pattern. They take a lot of complicated stuff and make it look like a simple function call.
I compiled it and everything's like I said. Change how you create Output then change how you call it. I also added a body for push so that it runs, I hope you don't mind.
//Program to demonstrate the Stack class
#include <iostream>
#include <vector>
using namespace std;
template<class T>
class Stack
{
public:
Stack();
bool empty() const;
T top() const;
T pop();
void push(T element) { pool.push_back( element ); }
void Output();
private:
vector <T> pool;
};
//constructor
template<class T>
Stack<T>::Stack()
{
pool.reserve(10);
}
//function to output the values in the stack
template<class T>
void Stack<T>::Output()
{
for ( int i = 0; i < pool.size(); ++i ) {
cout<< pool[i] <<endl;
}
}
int main()
{
Stack<double> dStack;
Stack<string> sStack;
cout << "Pushing 3.4, 5.5, 2.2, 6.7 -> on to dStack" << endl;
dStack.push(3.4);
dStack.push(5.5);
dStack.push(2.2);
dStack.push(6.7);
cout << endl;
cout << "Pushing Sawubona, Molo, Lotjha, Dumela, Hi, Hallo" << endl;
cout << "Ndi Masiari, Avuxeni -> on to sStack" << endl;
sStack.push("Sawubona");
sStack.push("Molo");
sStack.push("Lotjha");
sStack.push("Dumela");
sStack.push("Hi");
sStack.push("Hallo");
sStack.push("Ndi Masiari");
sStack.push("Avuxeni");
cout << endl << "dStack:" << endl;
dStack.Output();
cout << endl << "sStack:" << endl;
dStack.Output();
return 0;
}
Right. You prototype Output to take a T and try to pass a Stack<T>. I guess you'd call it like this for the two different functions I wrote.
// Output takes a T parameter
dStack.Output( dStack.top() );
// Output takes no parameters
dStack.Output();
Don't use atoi()
Why?
From the prototype I'd guess this.
template<class T>
void Stack<T>::Output(T element)
{
cout<< element;
}
I don't understand why you have a parameter. It seems you want to output the whole stack.
template<class T>
void Stack<T>::Output()
{
for ( int i = 0; i < pool.size(); ++i ) {
cout<< pool[i] <<endl;
}
}
All you want to do is find the maximum of each row? You already have a function that finds the maximum of a single row, why can't you call it on each row in a loop?
#include <cstdlib>
#include <iostream>
using namespace std;
int max( const int *array, const int count ) {
int Maximum = array[0];
for ( int i = 0; i < count; i++ ) {
if ( Maximum < array[i] ) {
Maximum = array[i];
}
}
return Maximum;
}
int main() {
int a[][5]= {
{3,41,5,6,7}
, {2,7,33,77,1}
, {9,3,7,1,8}
};
for ( int rows = 0; rows < 3; ++rows ) {
cout<<"Maximum: "<< max( a[rows], 5 ) <<endl;
}
return 0;
}
That's as close as I can get without giving you the answer on a plate.
Everybody doesn't think in code, and that example is pretty vague.
Will you please give me little detail hint?
When you loop over a 2D array, you use two loops, right? One loop on the outside goes over the rows and another on the inside that goes over the columns. You do it like this.
for ( int row = 0; row < nRows; ++row ) {
for ( int col = 0; col < nCols; ++col ) {
// do something with array[row][col]
}
}
When you do that it goes over the array like this with the numbers showing the order that you visit each cell.
[01][02][03][04][05]
[06][07][08][09][10]
[11][12][13][14][15]
That's because the outer loop handles the rows and the inner loop handles the columns. But if you reverse the loops so that the outer loop handles the columns and the inner loop handles the rows, it goes over the array like this.
[01][04][07][10][13]
[02][05][08][11][14]
[03][06][09][12][15]
You do that with code like this.
for ( int col = 0; col < nCols; ++col ) {
for ( int row = 0; row < nRows; ++row ) {
// do something with array[row][col]
}
}
What that loop does is pretend the columns are rows and kind of flips the array over and rotates it by by 90 degrees clockwise. But you do it without really flipping and rotating, you …
I read about a trick that makes everything together. Move the PreDefStruct member to be last, make it an array of 1, and then make the APISTRUCT variable dynamic. If you allocate the size of APISTRUCT plus the size of x PREDEFINEDSTRUCTs I think you get what you need.
#include <stdio.h>
typedef struct tagPredefinedStruct{
int iSize;
int iCount;
} PREDEFINEDSTRUCT;
typedef struct APIStruct {
int iNumber;
int iDate;
PREDEFINEDSTRUCT PreDefStruct[1];
} APISTRUCT;
int main( void ) {
APISTRUCT *api = malloc( sizeof( APISTRUCT ) + 3 * sizeof( PREDEFINEDSTRUCT ) );
api->PreDefStruct[0].iSize = 0;
api->PreDefStruct[1].iSize = 1;
api->PreDefStruct[2].iSize = 2;
printf( "%d\n", api->PreDefStruct[0].iSize );
printf( "%d\n", api->PreDefStruct[1].iSize );
printf( "%d\n", api->PreDefStruct[2].iSize );
free( api );
return 0;
}
Did you try to compile it? This can't be misspelling.
I just did to be sure. I got the same errors and changing privite to private fixed everything.
15 ISO C++ forbids declaration of `privite' with no type
The error says it all. private is misspelled.
Does this work for you?
#include <stdio.h>
typedef struct tagPredefinedStruct{
int iSize;
int iCount;
} PREDEFINEDSTRUCT;
typedef struct APIStruct {
int iNumber;
PREDEFINEDSTRUCT *PreDefStruct;
int iDate;
} APISTRUCT;
int main( void ) {
APISTRUCT api;
api.PreDefStruct = malloc( 3 * sizeof( PREDEFINEDSTRUCT ) );
api.PreDefStruct[0].iSize = 0;
api.PreDefStruct[1].iSize = 1;
api.PreDefStruct[2].iSize = 2;
printf( "%d\n", api.PreDefStruct[0].iSize );
printf( "%d\n", api.PreDefStruct[1].iSize );
printf( "%d\n", api.PreDefStruct[2].iSize );
free( api.PreDefStruct );
return 0;
}
However, it crashes when I try to assign a value to the data structure.
Check the pointer to see if it's 0. If it is then malloc failed to allocate the memory.
My friend said another way is to give it an initial size of the array then change that size later.
You can't. Array sizes have to be constant and they can't be changed. A variable sized array is what I showed you.
You can't set the size of an array at run time. You have to dynamically create the array from a pointer.
typedef struct tagPredefinedStruct{
int iSize;
int iCount;
} PREDEFINEDSTRUCT;
typedef struct APIStruct {
int iNumber;
PREDEFINEDSTRUCT *PreDefStruct;
int iDate;
} APISTRUCT;
APISTRUCT api;
int x;
x = GetNumberofStructs(); //x is known only at runtime.
api.PreDefStruct = malloc( x * sizeof( PREDEFINEDSTRUCT ) );
Is Cstring a class? If it lets you get an individual character and the size, you can do this easy.
char MiddleCharacter( Cstring string ) {
return string[string.length() / 2];
}
A char array string is the same way but you have to use strlen.
char MiddleCharacter( const char *string ) {
return string[strlen( string ) / 2];
}
No biggie, right? :)
Do you ever hear about people dying due to overexposure of campfire smoke?
I don't think people camp enough to die of campfire smoke like they do of cigarette smoke. ;)