Tech2 Forums - Powered by vBulletin

how to write this program in C++

This is a topic on how to write this program in C++ within the Programmers Corner forums, part of the Technology category; I am trying to figure out the logic for these programs but I can't figure out how to write them: ...

Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Dushyant Chauhan
  1. #1
    lI0
    lI0 is offline
    Uber Newbie
    Join Date
    May 2012
    Posts
    1
    Liked
    0 times
    Reputation points
    10

    how to write this program in C++

    I am trying to figure out the logic for these programs but I can't figure out how to write them:
    1.A contest requires teams of 5 numbers each. Write a program that asks for the no. of players and then gives the no. of teams and no. of players left.
    2.Program to input a given no of days and find the no. of years,weeks and days out of it.
    3.Accept a character between a to j and print the next 4 characters
    If anyone can help me with these three programs.
    Thanks

  2. #2
    Tech2 Members
    Join Date
    Feb 2012
    Posts
    12
    Liked
    2 times
    Reputation points
    10

    Re: how to write this program in C++

    Hi,

    I wrote these programs in notepad quickly as I was in office so time was short therefore there can be compiler or run time errors so kindly check. But I made them as accurate as possible.

    I Couldn't get your 1st point please elaborate.

    Answer to your 2nd need number of years weeks and days:

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    void main()
    {
    
    
    int i=0, j, k;
    
    cout<<"Enter the number of days";
    cin>>i;
    
    clrscr();
    
    cout<<i<<" days have:\n";
    
    j = i/365; /*for getting no.of years*/
    
    cout<<j<<" Years  ";
    
    j = i%365; /* For taking the remainder which will comprise for the no. of days in a week*/ 
    
    k = j/7;  /* for getting the no. of weeks*/
    
    cout<<k<<" weeks  ";
    
    j = j%7 /* For taking the remainder which will comprise for the no. of days*/ 
    
    cout<<j<<" days";
    
    getch(); /* For holding the output screen*/
    
    }
    
    
    
    Answer to your 3rd need of printing series:
    
    #include<iostream.h>
    #include<conio.h>
    
    void main()
    {
    
    char c;
    int i=0, j;
    
    cout<<"Enter the Character between a to j after which the characters are to be printed    ";
    cin>>c;
    
    clrscr();
    
    j=(int) c; /* typecasting as I wrote this program in notepad and was not sure whether auto conversion will take place in the If loop*/
    
    if(j < 65  && j > 106)  /* To check whether the given char is in valid range (ASCII Values corresponding to the characters)*/
    
    { 
    
    Cout<<"Invalid range! Please enter Valid Character between a to j!  ";
    
    Cin>>c;
          
    clrscr();
    
          j=(int) c; /* typecasting as I wrote this program in notepad and was not sure whether auto conversion will take place in the If loop*/
    
    
          if(j >= 65 && j <= 106)      /* Valid Check*/
    
                { 
    
                  while(i < 4) /*loop for printing the series*/
    
                        {
    
                         cout<<"The Sequence is : \n";
    
                         j = j + i; /*step for incrementing the characters*/
                         
                         c = (char) j;
                          
                         cout<<c<<" ";
    
                         i++;
                         } 
    
    
    }
    
    getch(); /*For holding the output window*/
    
    }



    Regards,
    Dushyant Chauhan
    Last edited by coolpcguy; 06-02-2012 at 08:28 AM. Reason: use [code] for code blocks
    hatZim likes this.

  3. #3
    Tech2 Members
    Join Date
    Apr 2007
    Location
    Milky Way
    Posts
    1,431
    Liked
    15 times
    Reputation points
    21

    Re: how to write this program in C++

    Quote Originally Posted by lI0 View Post
    I am trying to figure out the logic for these programs but I can't figure out how to write them:
    1.A contest requires teams of 5 numbers each. Write a program that asks for the no. of players and then gives the no. of teams and no. of players left.
    I don't like spoon-feeding, so am just listing the logic for the 1st one, since the other tow have obviously been covered in details.

    Since each teams can have 5 members, you divide the player by 5 and store only the integer part of the number. This is your total number of teams. Multiply the number obtained by 5 and then subtract it from the total number of players entered, to get the number players that aren't part of any teams. (Hint: there's a guard code to be implemented here to make the prog a bit intelligent, but I'll leave that to you to discover for yourself.)

    If you need any help, post back!
    ^ Hope this helped. :D

    My Game Collection:
    http://www.playfire.com/Morpheus3000/games/


  4. #4
    Tech2 Members avtar2008's Avatar
    Join Date
    Jun 2010
    Location
    chandigarh
    Posts
    401
    Liked
    16 times
    Reputation points
    18

    Re: how to write this program in C++

    @morpheus:liked your way of teaching.no to spoon feeding.

  5. #5
    Tech2 Members ksg91's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    72
    Liked
    6 times
    Reputation points
    12

    Re: how to write this program in C++

    Quote Originally Posted by Morpheus3000 View Post
    I don't like spoon-feeding, so am just listing the logic for the 1st one, since the other tow have obviously been covered in details.

    Since each teams can have 5 members, you divide the player by 5 and store only the integer part of the number. This is your total number of teams. Multiply the number obtained by 5 and then subtract it from the total number of players entered, to get the number players that aren't part of any teams. (Hint: there's a guard code to be implemented here to make the prog a bit intelligent, but I'll leave that to you to discover for yourself.)
    Or you can just take modulo 5 to the total players to find out remaining players.
    My Gear :Nokia N8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •