Codelab excercise 2- computer programming part 1

In summary, the code tries to swap the values of i and j , saves the original value of i in temp , and then assigns j 's value to i .
  • #1
goku3240
4
0

Homework Statement



1) Given three already declared int variables, i , j , and temp , write some code that swaps the values in i and j . Use temp to hold the value of i and then assign j 's value to i . The original value of i , which was saved in temp , can now be assigned to j .

2) Given the variables temperature and humidity , write an expression that evaluates to true if and only if the temperature is greater than 90 and the humidity is less than 10 .
3) Given the integer variables yearsWithCompany and department , write an expression that evaluates to true if yearsWithCompany is less than 5 and department is not equal to 99 .
4)Given the variables isFullTimeStudent and age , write an expression that evaluates to true if age is less than 19 or isFullTimeStudent is true.
5) Write a conditional that assigns true to the variable fever if the variable temperature is greater than 98.6
6)
Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90 .
7)
Write a conditional that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000 .
8) Write a conditional that multiplies the values of the variable pay by one-and-a-half if the value of the boolean variable workedOvertime is true.
9) Write an if/else statement that assigns true to the variable fever if the variable temperature is greater than 98.6 ; otherwise it assigns false to fever .
10) Write an if/else statement that compares the value of the variables soldYesterday and soldToday , and based upon that comparison assigns salesTrend the value -1 ( soldYesterday greater than soldToday ) or 1 .

11) Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18 , adds 1 to the variable adults if age is 18 through 64 , and adds 1 to the variable seniors if age is 65 or older.
12)
Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the bool variables neutral , base , and acid :
false, false, true if pH is less than 7
false, true, false if pH is greater than 7
true, false, false if pH is equal to 7
13) Write a switch statement that tests the value of the char variable response and performs the following actions:
if response is y , the message Your request is being processed is printed
if response is n , the message Thank you anyway for your consideration is printed
if response is h , the message Sorry, no help is currently available is printed
for any other value of response , the message Invalid entry; please try again is printed





Homework Equations



the control structures if and else, true flase,

The Attempt at a Solution



the first i tried temp=i then j=temp and i switched up the variables but haven't good the right answer. all the other ones are the same i can't get the 1st few to understand the rest.
 
Physics news on Phys.org
  • #2
Same comment as in your other thread: Per the forum rules, you need to make an effort at this yourself before getting help.
 
  • #3
Mark44 said:
Same comment as in your other thread: Per the forum rules, you need to make an effort at this yourself before getting help.

KK thanks I just joined today, and I just read the rules. So i will try it out but will i reiceve help with the difficult ones?
 
  • #4
Post 1 problem at a time, not 10 or a dozen, and show that you are making a serious attempt at writing the code for it. If you're still stuck, someone will steer you in the right direction.
 
  • #5




Hello, it seems like you are struggling with some of the coding exercises. That's completely normal, programming can be challenging at times. Here are some tips that may help you with these exercises:

1) For the first exercise, you are on the right track by using a temporary variable to swap the values of i and j. However, you also need to use the assignment operator (=) to assign the values to the correct variables. For example, temp = i; i = j; j = temp; This will swap the values of i and j.

2) For the second exercise, you can use the logical operator "&&" to combine the conditions for temperature and humidity. So the expression would be: temperature > 90 && humidity < 10.

3) For the third exercise, you can use the logical operator "&&" to combine the conditions for yearsWithCompany and department. Remember that the "not equal" operator is !=. So the expression would be: yearsWithCompany < 5 && department != 99.

4) For the fourth exercise, you can use the logical operator "||" to combine the conditions for age and isFullTimeStudent. So the expression would be: age < 19 || isFullTimeStudent.

5) For the fifth exercise, you can use the comparison operator ">" to compare the temperature variable with the value 98.6. So the conditional statement would be: if(temperature > 98.6){ fever = true; }.

6) For the sixth exercise, you can use the conditional statement "if" to check if the outsideTemperature is greater than 90. If it is, then you can use the assignment operator "-=" to subtract 4 from the shelfLife variable. So the conditional statement would be: if(outsideTemperature > 90){ shelfLife -= 4; }.

7) For the seventh exercise, you can use the conditional statement "if" to check if the goodsSold variable is greater than 500,000. If it is, then you can use the assignment operator "=" to assign the value 10,000 to the bonus variable. So the conditional statement would be: if(goodsSold > 500,000){ bonus = 10,000; }.

8) For the eighth exercise, you can use the conditional statement "if" to check if the workedOvertime variable is true. If it is, then you can use the assignment operator
 

Related to Codelab excercise 2- computer programming part 1

1. What is a Codelab exercise?

A Codelab exercise is an interactive programming exercise that allows users to practice and improve their coding skills. It typically consists of a set of instructions or tasks that must be completed using a specific programming language.

2. What is the purpose of Codelab exercise 2?

The purpose of Codelab exercise 2 is to introduce users to basic computer programming concepts and techniques. It covers topics such as variables, loops, and conditional statements, which are essential building blocks for any programming language.

3. How do I access Codelab exercise 2?

Codelab exercises can be accessed through various online learning platforms, such as Google's Codelabs or Codecademy. Simply search for "Codelab exercise 2 computer programming part 1" and you will find links to the exercise.

4. Do I need any prior programming knowledge to complete Codelab exercise 2?

No, Codelab exercise 2 is designed for beginners with little to no programming experience. It provides step-by-step instructions and explanations to help users understand the concepts and complete the tasks successfully.

5. Can I use any programming language for Codelab exercise 2?

No, Codelab exercise 2 specifically uses the Python programming language. However, the concepts and techniques covered can be applied to other programming languages as well.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
2
Replies
40
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Programming and Computer Science
Replies
4
Views
893
Back
Top