5 Replies - 327 Views - Last Post: 01 February 2019 - 03:53 PM Rate Topic: -----

#1 avnitoto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-January 19

how to add a if statment to this code

Posted 28 January 2019 - 01:51 AM

C++


When run
first row 1 2 3 4 5
second row 1 2 3 4 6
third row 1 2 3 4 7
fourth row 1 2 3 4 8
fifth row 1 2 3 4 9
sixth row 1 2 3 4 10
seventhrow 1 2 3 5 6
...
...
...


I cant add if statement when a = 1 , b =2 I want result
first row 1 3 4 5 6
second row 1 3 4 5 7
third row 1 3 4 5 8
fourth row 1 3 4 5 9
fifth row 1 3 4 5 10
sixth row 1 3 4 6 7
seventh row 1 3 4 6 8
...
...




#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   const int NMAX = 10;
   int i = 0;

   //Choose ONE of the following
    //ofstream out( "output.txt" );       // for file
   ostream & out = cout;                // for screen

   for ( int a = 1; a <= NMAX - 2; a++ )
   {
      for ( int b = a + 1; b <= NMAX - 1; b++ )
      {
         for ( int c = b + 1; c <= NMAX; c++ )
         {
            for ( int d = c + 1; d <= NMAX; d++ )
            {
            	for ( int e = d + 1; e <= NMAX; e++ )
            {
            	
            	
			
			int n = 100 * a + 10 * b + c + d + e;
            i++;
            //out << i << ":   " << n << '\n';
            out << i << ": " << a << " " << b << " " << c << " " << d << " " << e << '\n';
         }
      }
   }
 }
}
}

This post has been edited by andrewsw: 28 January 2019 - 01:56 AM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: how to add a if statment to this code

#2 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6745
  • View blog
  • Posts: 27,775
  • Joined: 12-December 12

Re: how to add a if statment to this code

Posted 28 January 2019 - 01:57 AM

There is no attempt at an if statement in that code. Post your attempt, or ask a question describing what is confusing you.

(Also, please wrap code in code tags when posting.)
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6745
  • View blog
  • Posts: 27,775
  • Joined: 12-December 12

Re: how to add a if statment to this code

Posted 28 January 2019 - 02:15 AM

@OP There is no need to quote your original post in full, there is a Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

#4 albert003   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 669
  • Joined: 15-December 14

Re: how to add a if statment to this code

Posted 30 January 2019 - 12:53 PM

Lets put your thinking cap on you want to use an if statement in your program. So ask yourself three simple questions...

1. When you made your flow charts where did you put the if statements?
2. When you decide where you want to put the if statement ask yourself does it make sense to have it there or is there a simpler solution to make the program work?.
3. Where would you put it in your program and why there?

Under the rules of this website we cannot give you the answers. You have to tell us what you have tried and show us the work you have done.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

Re: how to add a if statment to this code

Posted 30 January 2019 - 01:20 PM

Also please realize that you already have what amounts to several if() statements in your code, look at your loops, those loop condition statements are basically if() statements in disguise.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 hexagod   User is online

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: how to add a if statment to this code

Posted 01 February 2019 - 03:53 PM

frankly, I have absolutely no idea what you're asking for but if you want a result when a = 1 b = 2 then how about:

View Postavnitoto, on 28 January 2019 - 01:51 AM, said:

C++


#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   const int NMAX = 10;
   int i = 0;

   //Choose ONE of the following
    //ofstream out( "output.txt" );       // for file
   ostream & out = cout;                // for screen

   for ( int a = 1; a <= NMAX - 2; a++ )
   {
      for ( int b = a + 1; b <= NMAX - 1; b++ )
      {

         for ( int c = b + 1; c <= NMAX; c++ )
         {
            for ( int d = c + 1; d <= NMAX; d++ )
            {
            	for ( int e = d + 1; e <= NMAX; e++ )
            {
            	
            	
			
	    int n = 100 * a + 10 * b + c + d + e;
            i++;
            //out << i << ":   " << n << '\n';
            if (a == 1 && b == 2)
            {
              out << i << ": " << a << " " << b << " " << c << " " << d << " " << e << '\n';
            }
            else
            { 
              //nothing?
            }
         }
      }
   }
 }
}
}

This post has been edited by hexagod: 01 February 2019 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1