top of page
Click here to go to the home page of AskTheCode.

David is fighting Goliath (again) - MOOC assignment from Edx solution

MOOC assignment in C programming from Edx solution | C programming | AskTheCode

Problem:

Here is your final activity of this unit. Use it to apply everything you have learned! David is fighting Goliath (again...) and it turns out that Goliath is much bigger than David thought. Fortunately David is not short of resources and he plans to send robots to fight the giant. But before launching the assault, David must evaluate the performance of these robots to ensure success. This is where you come in. You are given some data on David's robots and need to compute and output a corresponding power score.


You should write a program that takes several lines of input from a user (see the below example). The first line of the input indicates the number of robots to be deployed. Each subsequent line shows the height, the weight, the power of the engines and a resistance rating (1,2, or 3) of each of of these robots. Your program should use this information to calculate the total power of these robots. The power of all robots is the sum of the power of each robot, where the power of an individual robot is calculated via:


(enginePower + resistance) * (weight - height)

Input:

  • The first line of the input contains a single integer R denoting the number of robots.

  • Each of next R lines, contains 4 integers, height, weight, enginePower, and resistance.

Output:

Print a single line containing the sum of the power of each robots.

Sample Input:

Enter the no. of robots to be deployed: 4
Enter the height, weight, enginePower, and the resistance for robot 1: 50 60 2 1
Enter the height, weight, enginePower, and the resistance for robot 2: 30 25 5 2
Enter the height, weight, enginePower, and the resistance for robot 3: 43 62 5 2
Enter the height, weight, enginePower, and the resistance for robot 4: 80 102 2 1

Sample Output:

The total power of these 4 robots is 194.

EXPLANATION:

Take input for each of the R robots from the user. Calculate the power of each robot, and keep adding them to get the required totalPower of the R robots.

Code:

#include <stdio.h>

int main(){
	int numOfRobots, height, weight, enginePower, resistance;
	printf("Enter the no. of robots to be deployed: ");
	scanf("%d",&numOfRobots);
	int totalPower = 0, i=1, num = numOfRobots;
	while(numOfRobots--){
		int powOfIndivRobot;
		printf("\nEnter the height, weight, enginePower, and the resistance for robot %d: ",i++);
		scanf("%d%d%d%d",&height,&weight,&enginePower,&resistance);
		powOfIndivRobot = (enginePower + resistance) * (weight - height);
		totalPower += powOfIndivRobot;
	}
	printf("\nThe total power of these %d robots is %d.",num, totalPower);
	return 0;
}

Note: Comment or remove out the irrelevant printf() to get the exact code🙂.

 
 
 

Recent Posts

See All

Comentarios


bottom of page