Question

In: Computer Science

I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...

I need to translate my java code into C code.


import java.util.Scanner;
class CS_Lab3 {

public static void main( String args[] ) {

Scanner input = new Scanner( System.in );
// create array to hold user input
int nums[] = new int[10];
int i = 0, truthCount = 0;
char result = 'F', result2 = 'F';

// ask user to enter integers
System.out.print("Please Enter 10 Different integers: ");

// gather input into array
for ( i = 0; i < nums.length; i++ )

nums[i] = input.nextInt();
  
/**********************************
Now I have the input. Remember that for all x P(x)
is equivalent to P(x1) ^ P(x2) ^ ... ^ P(xn)
and there exists an x P(x) is equivalent to P(x1) v P(x2) v ...
v P(xn).
***********************************/

// Here you check the variables against the given statement
// statement 1: For all x ((x < 0) --> x is even)
for ( i = 0; i < nums.length; i++ ) {

// pass values into lessThanZero method
if ( (lessThanZero( nums[i] )) == 'T' )
truthCount += 1;
  
} // end for

/*******************
you want to have truth count == 10 to show that all
values are true.
********************/
if ( truthCount == 10 )
System.out.println( "a) is True" );
  
else
System.out.println( "a) is False" );
  
truthCount = 0;
  
// start of checking nums against statement b
for ( i = 0; i < nums.length; i++ ) {

if ( (lessThanZero(nums[i])) == 'T' )
truthCount += 1;

} // end for

/**********************
You want truthCount > 0 to have a valid statement
***********************/
if ( truthCount > 0 )
System.out.println( "b) is True" );
  
else
System.out.println( "b) is False" );
  
truthCount = 0;

// start of checking nums against statement c
for ( i = 0; i < nums.length; i++ ) {

/*********
Here you want to check if the num[i] < 0 and num[i] / 2 == 0
so you want a method that will check to see if a number is even
**********/
if ( and( lessThanZero(nums[i]), isEven(nums[i]) ) == 'T' )
truthCount += 1;
  
} // end for

if ( truthCount > 0 )
System.out.println( "c) is True" );
  
else
System.out.println( "c) is False" );
  
truthCount = 0;

// start of checking nums against statement d
for ( i = 0; i < nums.length; i++ ) {

if ( or( divByThree(nums[i] - 1), divByTwo(nums[i] - 1) ) == 'T' )
truthCount += 1;

} // end for

if ( truthCount == 10 )
System.out.println( "d) is True" );
  
else
System.out.println( "d) is False" );

} // end main

public static char lessThanZero( int in_int ) {

char output;

if ( in_int < 0 )
output = 'T';
  
else
output = 'F';
  
return output;

} // end lessThanZero

public static char isEven( int in_int ) {

char output;

if ( in_int / 2 == 0 )
output = 'T';
  
else
output = 'F';
  
return output;

} // end isEven

public static char divByThree( int in_int ) {

char output;

if ( in_int % 3 == 0 )
output = 'T';
  
else
output = 'F';
  
return output;

} // end divByThree

public static char divByTwo( int in_int ) {

char output;

if ( in_int % 2 == 0 )
output = 'T';
  
else
output = 'F';
  
return output;

} // end divByTwo

public static char and ( char a, char b ) {

char andResult;

if ( a == 'T' && b == 'T' )

andResult = 'T';
  
else

andResult = 'F';
  
return andResult;

} // end and

public static char or ( char a, char b ) {

char orResult;
  
if ( a == 'F' && b == 'F' )

orResult = 'F';
  
else
  
orResult = 'T';
  
return orResult;
  
} // end or

} // end class

Solutions

Expert Solution

/* C code */

#include <stdio.h>

char lessThanZero( int in_int ) {
  
   char output;
  
   if ( in_int < 0 )
       output = 'T';
  
   else
       output = 'F';
  
   return output;

} // end lessThanZero

char isEven( int in_int ) {
  
   char output;
  
   if ( in_int / 2 == 0 )
       output = 'T';
  
   else
       output = 'F';
  
   return output;

} // end isEven

char divByThree( int in_int ) {
  
   char output;
  
   if ( in_int % 3 == 0 )
       output = 'T';
  
   else
       output = 'F';
  
   return output;

} // end divByThree

char divByTwo( int in_int ) {

   char output;
  
   if ( in_int % 2 == 0 )
       output = 'T';
  
   else
       output = 'F';
  
   return output;

} // end divByTwo

char and( char a, char b ) {
  
   char andResult;
  
   if ( a == 'T' && b == 'T' )
  
       andResult = 'T';
  
   else
  
       andResult = 'F';
  
   return andResult;

} // end and

char or ( char a, char b ) {
  
   char orResult;
  
   if ( a == 'F' && b == 'F' )
  
       orResult = 'F';
  
   else
  
       orResult = 'T';
  
   return orResult;
  
} // end or

int main()
{

   // create array to hold user input
   int nums[10];
   int i = 0, truthCount = 0;
   char result = 'F', result2 = 'F';
  
   // ask user to enter integers
   printf("Please Enter 10 Different integers: ");
  
   // gather input into array
   for ( i = 0; i < 10; i++ )
  
      scanf("%d",&nums[i]) ;
  
   /**********************************
   Now I have the input. Remember that for all x P(x)
   is equivalent to P(x1) ^ P(x2) ^ ... ^ P(xn)
   and there exists an x P(x) is equivalent to P(x1) v P(x2) v ...
   v P(xn).
   ***********************************/
  
   // Here you check the variables against the given statement
   // statement 1: For all x ((x < 0) --> x is even)
   for ( i = 0; i < 10; i++ ) {
  
       // pass values into lessThanZero method
       if ( (lessThanZero( nums[i] )) == 'T' )
           truthCount += 1;
  
   } // end for
  
   /*******************
   you want to have truth count == 10 to show that all
   values are true.
   ********************/
   if ( truthCount == 10 )
       printf( "a) is True " );
   else
       printf( "a) is False " );
  
   truthCount = 0;
  
   // start of checking nums against statement b
   for ( i = 0; i < 10; i++ ) {
  
       if ( (lessThanZero(nums[i])) == 'T' )
           truthCount += 1;
  
   } // end for
  
   /**********************
   You want truthCount > 0 to have a valid statement
   ***********************/
   if ( truthCount > 0 )
       printf( "b) is True " );
   else
       printf( "b) is False " );
  
   truthCount = 0;
  
   // start of checking nums against statement c
   for ( i = 0; i < 10; i++ ) {
  
       /*********
       Here you want to check if the num[i] < 0 and num[i] / 2 == 0
       so you want a method that will check to see if a number is even
       **********/
       if ( and( lessThanZero(nums[i]), isEven(nums[i]) ) == 'T' )
           truthCount += 1;
  
   } // end for
  
   if ( truthCount > 0 )
       printf( "c) is True " );
   else
       printf( "c) is False " );
  
   truthCount = 0;
  
   // start of checking nums against statement d
   for ( i = 0; i < 10; i++ ) {
  
       if ( or( divByThree(nums[i] - 1), divByTwo(nums[i] - 1) ) == 'T' )
           truthCount += 1;
  
   } // end for
  
   if ( truthCount == 10 )
       printf( "d) is True " );
  
   else
       printf( "d) is False " );

} // end main

/* PLEASE UPVOTE */


/* OUTPUT */


Related Solutions

My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
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...
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...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // As a hint, you should not have to use the name of each // month more than once. // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month...
import java.util.Scanner; public class Months { // You will need to write a method that makes...
import java.util.Scanner; public class Months { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month = input.nextInt(); String output = monthAsString(month); System.out.println(output); } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT