Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In this program the user has to insert two integer numbers: number1 which must be between 1 and 50 inclusive (i.e. 1<= number1 <=50) and number2 which must be between 51 and 100 inclusive (i.e. 51 <= number2 <= 100). A wrong inserted value for any number causes the output message "Invalid" to be displayed on the console and the program returns.

When valid numbers are entred, they are used to in a loop that applies the following equation in each iteration: k = i * i + 2 * j. In this loop, "i" represnts a series of consecutive ascending even numbers starting with the value of number1 if number1 is even or starting with the value of (number1 + 1) if number1 is odd. On the other hand, "j" represnts a series of consecutive descending odd numbers starting with the value of number2 if number2 is odd or starting with the value of (number2 - 1) if number2 is even. The loop terminates when "i" becomes greater than "j". After that, the exact average, maximum, and minimum values for all the k's calculated by the loop are printed out in three seperate lines.

For example, assume that the user inserted 45 and 59 as number1 and number2, respectively. The loop should start with i = 46 as the first even number and j = 59 as the first odd number. In the first iteration: k = 46*46 + 2*59. In the second iteration, i = 48, j = 57, and the second k = 48*48+ 2*57. In third teration,i = 50, j = 55, and the third k = 50*50 + 55*2. In fourth iteration i = 52, j = 53, and the fourth k = 52*52 + 2*53. The loop terminates after four iterations because (i = 54) > ( j = 51). The output will show 2518, 2810, and 2234 in three seperate lines as the average, maximum and minimum values for the four calculated k's, respectively.

What I have tried:

C++
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
    int N1, N2;
    int k, i, j;
    N1 = i;
    N2 = j;
    k = i * i + 2 * j;
    cin >> N1 >> N2;
    if (1 >= N1 <= 50 && 51 >= N2 <= 100);
    {
        (N1 % 2 == 0? int i = N1 + 1 : int i = N1))
        ((N2 % 2 != 0? int j = N2 - 1 : int j = N2)
    }
    else
        cout << "Invalid" << endl;
    while (i > j)
    {
        cout << k = i * i + 2 * j;
        i = i + 2;
        j = j - 2;
        double average = (N1 + N2)/2.0;
    }
    

    return 0;
}
Posted
Updated 22-Apr-21 10:17am
v2
Comments
Richard Deeming 22-Apr-21 6:04am    
You seem to have forgotten to ask a question.
rama barahmeh 22-Apr-21 6:53am    
it's not really a question it's more like there's a certain part that i didn't really know hot to do. idk if my code's right but i have no idea how i'm gonna do the max, min part.
mike@codeproject 22-Apr-21 14:52pm    
What I forgot to mention in my answer was that you should get the first part coded and fully tested before worrying about--or asking questions about--later parts of the assignment. It's pointless to talk about how to find the average/maximum/minimum of a sequence of k values when you haven't yet figured out how to generate those values. Step 1 is to get your input, step 2 is to use the input to generate the required sequence of k values. Get those done, in order if you don't want to go crazy, and ask about the first problem you can't solve on your own--not about the whole assignment.

You have quite a few problems. I'll address some of them, but the first thing I suggest for you to do is to not try to write whole programs at once. Break the task down into smaller problems, ideally into problems you know how to solve directly. (Sometimes you'll need to do a bit of research, though. That's normal.)

The first thing your assignment asks you to do is to read in two integers and check that they are valid. Simplify it even further: Read in just the value of number1 and verify that it's in range.

For example, if I wanted to read in an odd integer in the range 13-39 (inclusive) that might look like:

C++
int odd_number;
std::cin >> odd_number;
if (odd_number < 13 || odd_number > 39)
{
    std::cout << "Number must be in 13-39 range." << std::endl;
    return 1;
}
if (odd_number % 2 == 0)
{
    std::cout << "Number must be odd." << std::endl;
    return 1;
}


I used return 1; because a nonzero return value from main() conventionally indicates that there was an error. As you can see, I don't like "using namespace std;" much.

Your code should do something similar for both number1 and number2. Then print out the values you read in and validated (and return a 0 result for success). That's it. Don't even try anything else until that part is coded and tested.

Then you can move on to finding starting values for i and j; writing a loop that generates all pairs of (i, j) values from that starting point; computing the value of k for each of those pairs, and so on.

One thing you seem to be doing over and over again is treating the "=" operator as if it were a mathematical symbol. It's not. There are no equations in C++.

The statement k = i * i + 2 * j; is a statement that will compute a value for the expression (i*i + 2*j) using the current values of the variables i and j; and then store the result into the variable named k. Unless you have loops or conditional statements like if or switch, statements are executed sequentially. Your assignment statement will run just once, long before you've even given values to the variables i and j. The results will be meaningless.

You need to put your statements in the order that you want them to run.

Most of your code isn't valid C++ syntax. As you write each statement, check it against the documentation you have and make sure it's right. One particular thing to watch out for is a semicolon after the condition in an if statement There should be nothing (except spaces and maybe comments) between the closing ) of the condition and the statement (or braced block) it controls. You have:
if (1 >= N1 <= 50 && 51 >= N2 <= 100);
{ ...


The ; at the end of the condition is an error, one that I've made many times. It's hard to spot and usually not a syntax error. C++ has quite a few pitfalls like this that can be frustrating--especially while you are learning. The best advice I can offer for these types of issues is to find out how to turn on all warnings (--Wall for GNU/clang compilers, /W4 for Visual C++) and get in the habit of making all warnings go away before trying to test.
 
Share this answer
 
Comments
rama barahmeh 22-Apr-21 7:34am    
Thank you
CPallini 22-Apr-21 7:37am    
5.
Richard MacCutchan 22-Apr-21 7:44am    
+5 for an excellent description. Although I don't like those multiple comparisons in that if statement. I think expressions like 1 >= N1 <= 50 are suspect.
mike@codeproject 22-Apr-21 11:46am    
Thanks for the vote! Are you going to write two if statements on an array bounds test, though? I'd use (i < 0 || i >= n) every time. It's a pretty standard pattern for a "not in interval" test.
Richard MacCutchan 22-Apr-21 12:36pm    
Yes, I'm with you on that.

My previous comment was merely pointing out the the OP's statement was incorrect. I wonder who teaches them this stuff?
The issues with the earlier if statement have been pointed out. These two lines also have problems :
C++
(N1 % 2 == 0? int i = N1 + 1 : int i = N1))
((N2 % 2 != 0? int j = N2 - 1 : int j = N2)
The idea to use the ternary operator is a good one but your syntax is incorrect. You have already declared i and j so you do not want to do it again. Often the ternary operator is used for conditional assignment. This is how that looks :
C++
i = ( N1 % 2 == 0 ) ? N1 + 1 : N1;
j = ( N2 % 2 != 0 ) ? N2 - 1 : N2;
Here is another way to write the code that does not use the ternary operator :
C++
i = N1;
if( N1 % 2 == 0 )
    ++N1;
j = N2;
if( N2 % 2 == 0 )
    --N2;
One other thing - you declared the variable average in the while loop but it is not used anywhere. If you want to use it outside the loop then it has to be declared outside the loop.

ETA: the expression to calculate average has nothing to do with the variables i and j in the loop so it should not be there. Since you don't do anything with it I don't know why it even exists.
 
Share this answer
 
v2
Comments
rama barahmeh 22-Apr-21 18:17pm    
Thank you this was really helpful.
spidysmurf 26-Apr-21 16:35pm    
Can you share the code that eventually worked for you ?
Also is this by any chance the assignment for the Jordan university (Computer skills for engineers ), because it seems we have the same exact assignment
rama barahmeh 27-Apr-21 5:00am    
hi, yes this is the assignment, I actually still didn't get the final answer but if you want i can help you by explaining
spidysmurf 27-Apr-21 6:17am    
It's frustrating, i think i have the logic of the code right i just need someone to connect the dots for me. how can i send you my code ? or perhaps you can send me an email at ( spidysmurf@gmail.com) and ill replay with my code.
I still have not yet to take a look at the other two homeworks...
So modify your loop to look at the numbers and compare them against the current max and min values.
Then after your loop, print them.
Think about how you'd do it manually, and you'll see what I mean.

Nobody here is going to do your homework - even something this simple - and just give you code you can hand in as your own work.
 
Share this answer
 
Comments
rama barahmeh 22-Apr-21 7:09am    
I'm not looking for someone to solve my home work for me if i wanted to i could've paid someone to do it for me, there's a million ways to get someone to solve my homework for me I'm just having difficulties with the last part and i wanted to see if someone could help explain it to me
OriginalGriff 22-Apr-21 7:13am    
So what part of it do you want explained? I thought the question your teacher gave you was pretty clear myself - what part is giving you difficulties?
rama barahmeh 22-Apr-21 7:16am    
how do I put the maximum and the minimum do I put them inside the loop also he's asking for like the max of all k values right? (I've solved to most part on my own)
OriginalGriff 22-Apr-21 7:40am    
Think about it: I give you a page with one number per line: how do you find the max and min value manually?

Simple: you have two "mental variables" which hold the max and min so far.
You look at each line in turn and ask yourself "is this bigger than my current maximum?" If it is, you set your current max to that.
You do much the same for minimum.
When you get to the end of the page, you have the largest and smallest values on it as your "mental variables".

So yes, you do want to "do it in the loop" and print them after.
CPallini 22-Apr-21 7:38am    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900