[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Function Declaration in g++
From: |
BobR |
Subject: |
Re: Function Declaration in g++ |
Date: |
Sun, 02 Sep 2007 23:37:11 GMT |
<derrick_chi@msn.com> wrote in message...
> Hello guys
> I am new to this forum and to gcc/g++ and I need to know exactly
> how to declare a function in g++.
Your code shows signs of an 'C' programmer. I'll make some suggestions
(which might help you find the real problem).
>
#include <iostream> // #include "iostream"
#include <iomanip> // #include "iomanip"
// unused #include <cmath> // #include "math.h"
>
> using namespace std;
Not 'bad', but, not good. <G>
>
> unsigned int GCD_FUNCT( unsigned int, unsigned int );
>
> int main() {
>
> unsigned int ARRAY[32], GCD[16];
Any chance you could use 'std::vector' here? (see below)
// std::vector<unsigned> Array( 32 );
// std::vector<unsigned> Gcd( 32 );
// > unsigned int Loop_Control_V, Reg1, Reg2, new_value;
In C++, you don't need to declare all your vars at the top. Put them as
close to use as possible (and initialize them).
>
> for ( unsigned int i = 0; i < 31; ++ i ) {
>
// > new_value = i + ( 3 * (4) );
// 'new_value' not used outside this loop, so:
unsigned int new_value( i + ( 3 * (4) ) );
>
> if ( new_value % 2 != 0 ) {
// > new_value = new_value * 4;
new_value *= 4; // another way to write the above
> }
> else{
// > new_value = new_value * 10;
new_value *= 10;
> }
> ARRAY[i] = new_value;
> } // for(i)
>
// > Loop_Control_V = 0;
unsigned int Loop_Control_V(0);
unsigned int Reg1(0), Reg2(0);
>
> while ( Loop_Control_V < 30 ){
> if( ARRAY[Loop_Control_V] >
> ARRAY[Loop_Control_V + 1] ) {
> Reg1 = ARRAY[Loop_Control_V];
> Reg2 = ARRAY[Loop_Control_V + 1];
> }
> else {
> Reg1 = ARRAY[Loop_Control_V + 1];
> Reg2 = ARRAY[Loop_Control_V];
> }
>
> GCD[Loop_Control_V] = GCD_FUNCT(Reg1,Reg2);
BZZzzzzt!!! GCD is only [16], Loop_Control_V is going to 29!
// > Loop_Control_V = Loop_Control_V + 1;
++Loop_Control_V;
> } // while(Loop_Control_V)
>
> return 0;
> } // main()
>
> unsigned int GCD_FUNCT( unsigned int REGa, unsigned int REGb){
// > unsigned int temp1, temp2, GCD;
// > temp1 = REGa;
// > temp2 = REGb;
unsigned int temp1( REGa ), temp2( REGb );
> while ( temp1 != temp2 ) {
> if ( temp1 > temp2 ) {
// > temp1 = temp1 - temp2;
temp1 -= temp2;
> }
> else{
// > temp2 = temp2 - temp1;
temp2 -=temp1;
> }
> } // while(temp1)
// > GCD = temp1;
// > return GCD;
return temp1;
> } // GCD_FUNCT(unsigned unsigned)
>
// - using vectors -
#include <iostream>
#include <vector>
#include <iterator> // stream_iterator
#include <algorithm> // copy
unsigned int GCD_FUNCT( unsigned int REGa, unsigned int REGb){
while( REGa != REGb ){
if( REGa > REGb ){ REGa -= REGb;}
else{ REGb -= REGa; }
} // while(REGa)
return REGa;
} // GCD_FUNCT(unsigned unsigned)
int main(){
std::vector<unsigned> Array( 32 );
std::vector<unsigned> Gcd( 32 );
for( unsigned int i(0); i < Array.size(); ++ i ){
Array.at( i ) = i + ( 3 * 4 );
if( Array.at( i ) % 2 != 0 ) { Array.at( i ) *= 4; }
else{ Array.at( i ) *= 10; }
} // for(i)
unsigned int Loop_Control_V(0);
while ( Loop_Control_V < 30 ){
if( Array.at( Loop_Control_V ) >
Array.at( Loop_Control_V + 1 ) ){
Gcd.at( Loop_Control_V ) =
GCD_FUNCT( Array.at( Loop_Control_V ),
Array.at( Loop_Control_V + 1 ) );
} // if(>)
else{
Gcd.at( Loop_Control_V ) =
GCD_FUNCT( Array.at( Loop_Control_V + 1 ),
Array.at( Loop_Control_V ) );
} // else{<}
++Loop_Control_V;
} // while(Loop_Control_V)
std::copy( Gcd.begin(), Gcd.end(),
std::ostream_iterator<unsigned int>( std::cout, " " ) );
// alt: std::ostream_iterator<unsigned int>( std::cout,
"\n" ) );
std::cout<<std::endl;
return 0;
} // main()
--
Bob R
POVrookie
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: Function Declaration in g++,
BobR <=