Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-11-2004, 09:49 AM   #1 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
Is there anyting wrong with this?

Code:
#include <iostream>
#include <cstdlib>

using namespace std;
int oddy (int x(1), int y);
int main()
  oddy;
  cout<< "\n";
          
  system("PAUSE");  
  return 0;
}

int oddy (int x, int y)
{
  while (x<=10)
  {
    y = x % 2;
    if (y==1)
    {
      cout << x << " is odd" << endl;
      x++;
    }
      else
    {
        cout << x << " is even" << endl;
      x++;
    }
  }
  return 0
}

Last edited by Valmont; 10-11-2004 at 01:07 PM.
Androto is offline   Reply With Quote
Old 10-11-2004, 01:28 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
A lot .
But first thing first. Try to use the CODE tags. Insert your code between [ code ] and [ /code ].
Here, compare these:
Code:
#include <iostream>
#include <cstdlib>

using namespace std;

void odd();
void odd(int x);

int main()
{
   odd();
   cout<<endl;
   
   odd(4);
   cout<<endl;
   
   system("PAUSE");
   return 0;
}

void odd()
{
   int x(0);
   while (x<=10)
   {
      if (x % 2 == 0)
         cout << x << " is even";
      else
         cout << x << " is odd";
      { ++x; cout<<endl; }
   } 
}

void odd(int x)
{
   if (x % 2 == 0)
      cout << x << " is even";
   else
      cout << x << " is odd";
}
The usage of these two functions are different, although they are fundamentally the same functions. If I could remove the double code, it will show. The core of the application is if (x % 2 == 0). So if could extract this test from both the functions, I could make things a little less memory consuming without critical speed (if any speed is lost to start with). And besides, we'd like to seperate messages from the critical functions. So I'll do that too. Later, when you build controllers and engines and alike (these do the "clever" part of a program), you'd like to seperate the views from everything else.
Code:
#include <iostream>
#include <cstdlib>

using namespace std;

bool odd(int x);
void odd_even_msg(bool m);
void range_test_odd();

int main()
{
   range_test_odd();
   system("PAUSE");
   return 0;
}

//////////////////////////////////////

bool odd(int x)
{
   if (x % 2 == 0)
      return false;
   else
      return true;
}

//////////////////////////////////////

void odd_even_msg(bool m)
{
   if(m)
      cout<<"is even"<<endl;
   else
      cout<<"is odd"<<endl;
}

/////////////////////////////////////////

void range_test_odd()
{
   for(int i = 0; i<11; ++i)
   {
      cout<<i<<' ';
      odd_even_msg( odd(i) );
   }
}
__________________

Last edited by Valmont; 10-11-2004 at 02:14 PM.
Valmont is offline   Reply With Quote
Old 10-11-2004, 01:32 PM   #3 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
yes, many things.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;
//LONE BELOW: cannot declare a default value on a parameter when the following parameters do
// not have defaults.  this header does not match the function declaration
int oddy (int x(1), int y); 

int main() // opening brace?
  oddy; // this statement does nothing
  cout<< "\n"; // endl is generally preferred

  system("PAUSE");  // this doesn't work on my system.
  return 0;
}

// why is 'y' a parameter to oddy if its passed in value is not being used?
int oddy (int x, int y)
{
  while (x<=10)
  {
    y = x % 2;
    if (y==1)
    {
      cout << x << " is odd" << endl;
      x++;
    }
      else
    {
        cout << x << " is even" << endl;
      // x is incremented in two places.  why not have
      x++;  //a single increment outside the if/else statements?
    }
  }
  // why does this function return a value?  it does not compute
  //anything and does not detect any possible failures
  return 0 // semicolon
}
of course, compiling this would have told you most of these. is this what they give as college level homework assignments nowadays?

Last edited by Valmont; 10-11-2004 at 01:59 PM.
joe_bruin is offline   Reply With Quote
Old 10-11-2004, 01:36 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Joe, I've reformatted your reply a bit. It had a width of over 140.
__________________

Last edited by Valmont; 10-11-2004 at 02:00 PM.
Valmont is offline   Reply With Quote
Old 10-11-2004, 05:38 PM   #5 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
Quote:
Originally posted by Valmont
Joe, I've reformatted your reply a bit. It had a width of over 140.
you can do that?




thx for the corrections.
Androto is offline   Reply With Quote
Old 10-11-2004, 05:41 PM   #6 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
Quote:
is this what they give as college level homework assignments nowadays?"
no, thats what they give high school level homework assignments.
Androto is offline   Reply With Quote
Old 10-12-2004, 01:59 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally posted by Androto
you can do that?




thx for the corrections.
You're not Joe. I mean Joe Bruin. lol
__________________
Valmont is offline   Reply With Quote
Old 10-12-2004, 06:40 PM   #8 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
i know i'm not joe, but i just wanted to know if you could actually change other peoples' posts?
Androto is offline   Reply With Quote
Old 10-12-2004, 07:50 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes, I'm a moderator .
__________________
Valmont is offline   Reply With Quote
Old 10-12-2004, 10:44 PM   #10 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Not just a moderator, the moderator, hence the super moderator over the avatar.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Something wrong with the view script redhead Lounge 1 10-11-2004 01:58 PM
whats wrong with this line? trevor PHP 8 12-05-2003 08:45 AM
whats wrong with this code trevor PHP 7 04-01-2003 06:00 PM
ok, what is wrong with this code? trevor PHP 3 02-04-2003 10:09 PM


All times are GMT -8. The time now is 09:48 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting