Question

In: Computer Science

Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for...

Can someone convert this to C++ Please!

import java.util.*; // for Random
import java.util.Scanner; // for Scanner
class game{
   public static void main(String args[])
   {
       // generating a random number
       Random rand = new Random();
       int code = rand.nextInt(99999) + 1, chances = 1, help, turn, i,match, sum;
      
       // for input
       Scanner sc = new Scanner(System.in);
      
       // running for 10 times
       while(chances < 11)
       {
           System.out.print("\nYour chance number "+chances+" : ");
          
           // taking user input
           turn = sc.nextInt();
          
           // if the code matches, comes out of loop
           if(turn == code)
                   break;
           // code didn't match
           else
           {
               help = code;
               match = 0;
               sum = 0;
              
               // looping 5 times to check each digit
               for(i=0;i<5;i++)
               {
                   // Checking from last digit to first digit. So dividing by 10 each time.
                   // if digit is matched, adding it to sum and incrementing match variable by 1
                   if(help%10 == turn%10)
                   {
                       match++;
                       sum+=help%10;
                   }
                   help = help/10;
                   turn = turn/10;
               }
              
               // printing sum and match to guide player
               System.out.printf("\nNumber of digits matched: %d\nSum of them : %d\n", match, sum);
           }
           chances++;
       }
      
       // printing relavant message
       if(chances == 11)
       {
           System.out.println("\nYou lost! Code is "+code);
       }
       else
           System.out.println("\nCongratulations, you won!");
   }
}

Solutions

Expert Solution

#include <iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    // generating a random number
    srand(time(NULL));
    int code = rand() % 99999 + 1, chances = 1, help, turn, i, match, sum;

    // running for 10 times
    while (chances < 11) {
        cout << "\nYour chance number " << chances << " : ";

        // taking user input
        cin >> turn;

        // if the code matches, comes out of loop
        if (turn == code)
            break;
            // code didn't match
        else {
            help = code;
            match = 0;
            sum = 0;

            // looping 5 times to check each digit
            for (i = 0; i < 5; i++) {
                // Checking from last digit to first digit. So dividing by 10 each time.
                // if digit is matched, adding it to sum and incrementing match variable by 1
                if (help % 10 == turn % 10) {
                    match++;
                    sum += help % 10;
                }
                help = help / 10;
                turn = turn / 10;
            }

            // printing sum and match to guide player
            cout << "\nNumber of digits matched: " << match << "\nSum of them : " << sum << "\n";
        }
        chances++;
    }

    // printing relavant message
    if (chances == 11) {
        cout << "\nYou lost! Code is " << code << endl;
    } else
        cout << "\nCongratulations, you won!" << endl;
}

Related Solutions

Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public class Number{ public static void main(String[] args) {    Scanner scan = new Scanner(System.in); System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement int[] array = new int[20]; //array of size 20 for(int i=0;i<20;i++){ array[i] = scan.nextInt(); //user input if(array[i]<-999 || array[i]>999){ //check if value is inside the range System.out.println("Please enter a number between -999 to 999"); i--; } } //...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT