Thanks Anchovy. The increments i and if loop continues running until you drop out of the for loop or a[i] > a[i+1]. I got my original code to work correctly, I used braces for the body of the if loop, but got all out of wack/confused when I tried his code.
Thank you AlephZero and jbunn. Ok, not to sound defensive but the body of function was cut and pasted from the answer in the book. I actually had different code and was trying to understand why they used the code they did. The question directly from the book:
Write a fxn named out of order...
I must admit I don't understand why the book answer has in the for loop i < size - 1 rather that just size. If I have an array of five spaces I need the loop to execute i 0 through 4.
a[0] = 2
a[1] = 7
a[2] = 9
a[3] = 8
a[4] = 12
1st loop
Step 1: i = 0 so
Step 2: check that i < size(5) - 1...
#include <iostream>
using namespace std;
int out_of_order(int a[], int size);
const int amount_of_numbers = 5;
int main()
{
int numbers;
int a[] = { 2, 7, 9, 8, 12 };
int number_out_of_order = out_of_order(a, amount_of_numbers);
cout << number_out_of_order;
}
int...
Thanks. amount_of_numbers = 5 at the top. Problem: As my current array is arranged 7 > 6 and therefore 6 should be shown on the screen but I keep getting -1. My original idea was to build this as the answer in the book was given
for (int i = 0; i < size - 1; i++)
if (a[i] > a[i + 1])...
Hi there- I'm trying to check an array for ascending order and if not to print out the number that is out of order by using a function. I solved it by not using a function but when I've tried to use a fxn for some reason it tells me that in my call for the fxn the numbers "argument of type int...
Basically I want to take an unknown amount of variables and sum them up. I'm sure it's simple. I know there is some way to tell VS to keep reading the opened stream from my numbers.txt file.
#include <iostream>
#include <fstream>
using namespace std;
ifstream data_input...
The return value is correct, -40. I have a feeling getting loops to run the correct amount of times is going to be tricky topic for me for a while. Thanks to both of you.
thanks cpscdave, I added your print line code and obviously it doesn't stop @ -40. I don't get it.
c_temp: -34 vs f_temp: -29
c_temp: -35 vs f_temp: -31
c_temp: -36 vs f_temp: -32
c_temp: -37 vs f_temp: -34
c_temp: -38 vs f_temp: -36
c_temp: -39 vs f_temp: -38
c_temp: -40 vs f_temp: -40...
I must write a program that finds the temperature, as an integer, that is the same in both Celsius and Fahrenheit. The formula for this is
F=(9/5)C+32
The program should create two integer variables for the temperature in Celsius and Fahrenheit. Initialize the temperature to 100 degrees...