how to auto test a C program use files? - testing

I have a C program and I need to test it using a file containing some test data and output the results automatically. How can I do this? Should I modify the program or should I write another program using C or something else like Python? Which is easier?
Here is my C program:
int main()
{
double wh,sh,salary;
int num = 0;
int valid = 1;
printf("Please input two non-negative numbers as a employee's working hours in one week and salary per hour.\n");
printf("Notice that the salary per hour can't be greater than 1000\nAlso you can't input more than 10 sets of data\n");
printf("Input them like this:\n20,34\nthen enter the Enter key\n");
while(scanf("%lf,%lf",&wh,&sh) == 2 && num < 10)
{
if(sh <0 || wh <0)
{
printf("Salary per hour or salary per hour can't be negative!\n");
}
else if(wh > 168)
{
printf("Working hours in one week can't be more than 168!\n");
}
else if(sh > 1000)
{
printf("Salary can't be greater than 1000!\n");
}
else
{
num ++;
if(wh <= 40)
{
if(sh <= 1000)
{
salary = wh * sh;
}
else if(sh > 1000 )
{
salary = wh * 1000;
}
}
else if(wh > 40 && wh <= 50)
{
if(sh*1.5 < 1000)
{
salary = 40*sh + (wh-40)*1.5*sh;
}
else if(sh*1.5 > 1000 && sh < 1000)
{
salary = 40*sh + (wh-40)*1000;
}
else if(sh > 1000)
{
salary = wh * 1000;
}
}
else if(wh > 50)
{
if(sh*3 < 1000)
{
salary = 40*sh + 10*1.5*sh + (wh-50)*3*sh;
}
else if(sh*1.5 < 1000 && sh*3 >=1000)
{
salary = 40*sh + 10*1.5*sh + (wh-50)*1000;
}
else if(sh*1.5 >= 1000 && sh <= 1000)
{
salary = 40*sh + (wh-40)*1000;
}
else if(sh > 1000)
{
salary = wh*1000;
}
}
printf("The total working hours is %lf, and salary per hour is %lf, salary is %lf\n",wh,sh,salary);
}
}
return 0;
}
For this moment please ignore these literals floating around.
I suppose the file containing test data is like this:
1,2
34,34
67,43
...
I really have no idea how this test automation works,please help!

in cmd prompt
enter
c:\>myprog.exe < input.txt > output.txt
put all ur testcases in input.txt
ur output will print in the output.txt file

Related

how to write unit test cases in jasmine for this code?

private getTotalMinutesBetweenStartAndEnd(startTime: string, endTime: string): number {
// get each time's hour and min values
let [startHrs, startMins] = this.getHoursAndMinsFromTime(startTime);
let [endHrs, endMins] = this.getHoursAndMinsFromTime(endTime);
// time arithmetic (subtraction)
if (endMins < startMins) {
endHrs -= 1;
endMins += 60;
}
let mins = endMins - startMins;
let hrs = endHrs - startHrs;
// this handles scenarios where the startTime > endTime
if (hrs < 0) {
hrs += 24;
}
return hrs * 60 + mins;
}

Why doesn't this While loop work as intended?

Hey guys, newbie here. One question, can't understand why this while loop doesn't work even when I entered a int bigger than 9 to the variable num, the while loop should repeat itself until the expression is false, and it doesn't, no output even. Am I missing something here? Thanks in advance.
fun main () {
while(true) {
println ("\nWrite a positive number: ")
var num = readLine()!!.toInt()
var sum = 0
if (num > 9) {
while (num > 9) {
var digit = num % 10
sum = sum + digit
num = num / 10
}
println("\nDigit Sum: $sum")
} else if (num in 1..9) {
println("\nDigit Sum for the number $num is $num")
} else {
println("\nInvalid input, try again.")
}
}
}
The issue is that you are not summing the last num when it gets less or equal to 9. You can even simplify your code a bit. Try the following:
fun main() {
while(true) {
println ("\nWrite a positive number: ")
val insertedNumber = readLine()!!.toInt()
var num = insertedNumber
var sum = 0
while (num > 9) {
val digit = num % 10
sum = sum + digit
num = num / 10
}
sum = sum + num
println("\nDigit Sum for the number $insertedNumber is $sum")
}
}
You don't need to redeclare the variables every time
var sum = sum + digit
var num = num / 10
So simply remove var
sum = sum + digit
num = num / 10

I would like add GPA in my code but unfortunately not working

I would ask if someone can help me the GPA in my code not working as per required.please find below it.
Scanner Student=new Scanner(System.in);
System.out.println("Please Enter Three Marks for Student");
int sum=0;
double avg;
int[]x=new int[3];
for (int i = 0; i < x.length; i++) {
System.out.println("The mark : "+ ( i +1));
x[i]=Student.nextInt();
sum=sum+x[i];
}
char grade;
if (sum >= 90 )
{
grade = 'A';
}
else if (sum >= 80 )
{
grade = 'B';
}
else if (sum >= 70)
{
grade = 'C';
}
else if (sum >= 50)
{
grade = 'D';
}
else
{
grade = 'F';
}
// Display the grade.
System.out.println("The grade is " + grade);
avg=sum/x.length;
System.out.println("The total of three marks are :"+sum+"\nThe Average is :"+avg);
}

Calculate average packet travel speed in NS2 using AWK

I need to calculate average packet travel speed in ns2. I have to write this formula in awk program. But I have no idea how to do it.
n is the number of received packet, and s is the distance when the packet is transmitted. Any answer will be very helpfull. Thanks.
function topla( array ) {sum=0; for (i in array) {sum=sum+250/array[i];}return sum;}
BEGIN {
total_packets_sent = 0;
total_packets_received = 0;
total_packets_dropped = 0;
first_packet_sent = 0;
last_packet_sent = 0;
last_packet_received = 0;}
{event = $1; time = $2; node = $3; type = $4; reason = $5; packetid = $6;
if ( time < simulation_start || simulation_start == 0 )
simulation_start = time;
if ( time > simulation_end )
simulation_end = time;
if ( type == "AGT" ) {
nodes[node] = node; # to count number of nodes
if ( time < node_start_time[node] || node_start_time[node] == 0 )
node_start_time[node] = time;
if ( time > node_end_time[node] )
node_end_time[node] = time;
if ( event == "s" ) {
flows[node] = node; # to count number of flows
if ( time < first_packet_sent || first_packet_sent == 0 )
first_packet_sent = time;
if ( time > last_packet_sent )
last_packet_sent = time;
# rate
packets_sent[node]++;
total_packets_sent++;
# delay
pkt_start_time[packetid] = time;
}
else if ( event == "r" ) {
if ( time > last_packet_received )
last_packet_received = time;
# throughput
packets_received[node]++;
total_packets_received++;
# delay
pkt_end_time[packetid] = time;
}
}}`
END {
# delay
for ( pkt in pkt_end_time) {
end = pkt_end_time[pkt];
start = pkt_start_time[pkt];
delta = end - start;
if ( delta > 0 ) {
delay[pkt] = delta;
}
}
result=topla(delay)/total_packets_received;
printf(result)
}
I write this awk program. But it gives the division by zero attempted error.And I wrote the transmission range in the topla function as 250, but I did not get the results I wanted.How should I write the transmission range?

Encountering problems with JSFiddle code

I have recently written a piece of code that checks temperature over a period of time for some course work. i wrote the program using different bits of code but cannot find the issue to some of the problems i am encountering.
I was just wondering if anyone can see any obvious errors that i am missing and could help out with some information int he right direction.
Link to the JSFiddle
//Counting the average temperatures in a day
//needs debugged!!!
var temperatures = [];
var total = 0;
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
function getNumDays() {
number = prompt("How many days in the study? Enter 1 - 10");
while ((number < 1) || (number > 10) ||( isNaN(number) === true)) {
alert ("Invalid input!");
number = prompt ("Enter again, 1 - 10 >> ");}
return number;
}
function getTemps(numDays) {
total = 0;
for (i = 0; i < numDays; i++) {
next = prompt ("Enter the temperature for day " + (i+1));
next = parseint(next);
while (isNaN(next)===true) {
next = 0;
next = prompt ("Error in input! Try again >>");
next = parseInt(next);}
temperatures.push(next);
}
total = total + next;
return temperatures;
}
function calcAverage(total, numDays) {
average = total / numDays;
return average;
}
function showStatistics(city, average, numdays) {
alert ("The average daily temperature for "+ city + " is " + average.toFixed(2) + " measured over " + numDays + " days." );
}
//main program
city = getCity();
numDays = getNumDays();
temperatures = getTemps(numDays);
Average = calcAverage(total, numDays);
showStatistics(city, average, numDays);
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
//main program
city = getCity();
Looks like you're missing a return statement.
Additionally, the line total = total + next; seems to be out of place: I imagine the total to be the total of the temperatures, not 0 + temperatureOfLastDay.