In: Computer Science
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
/* 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 */