converting a for loop to a while loop
Would you mind elaborating?
balamw said:I think lloyddean was asking in what way you messed that up. Did you get the exit condition wrong? The increment location? ...
B
If you need help with your homework, read your C++ book, or ask your teacher or a fellow student.
A few hints:
i) read about arrays in C++
ii) put the code in your program, hit compile. If the compiler throws an error, the code is probably wrong
iii) A function has to return the type that it was defined as, e.g. float has to return a float value, void shouldn't return anything
I'm surprised that all function examples pass the reference to the variable. Can someone explain why this would be good/bad?
i feel like 1, 3 and 5 in the first question i posted are correct, but theres no option for them. UGH this is so frustrating.
jk 3 is wrong isnt it?
The array is defined as having length 4, but is initialized with an array of length 5.
A few hints:
I'm surprised that all function examples pass the reference to the variable. Can someone explain why this would be good/bad?
http://www.cplusplus.com/doc/tutorial/arrays/
<return-type> square(const int &x)
<return-type> square(int const x)
it threw me off for a second because i know the index starts at 0, but i get it now.
so am i correct in saying that 1 and 5 are the only correct ones, would you agree? i really hope im right. lol
and the other questioni posted i THINK im right in saying the answer is d. so again, thanks for the helpppp
2 Given the following array declarations:
(1) double a[30];
(2) int s(6) = {1, 2, 3, 4, 5, 6};
(3) int s[4] = {1, 2, 3, 4, 5};
(4) char[30] r;
(5) int s[] = {1, 2, 3, 4, 5};
(6) char str[] = hello;
Which of the following statements is correct?
(a) (2), (3) and (4) are wrong and the rest are correct.
(b) Only (1) and (6) are correct.
(c) Only (1) and (5) are correct.
(d) None of the above.
I hate multiple choice questions. Why not let the student mark each of 1 to 6 as correct or incorrect?
(1) is correct. Just an array of thirty doubles.
(2) is wrong. Uses the wrong brackets.
(3) is wrong. Too many initialisers.
(4) is just horrible. I don't care whether it is right or not, you should never write this. I think it's wrong. (That's the difference between language lawyer and developer. I don't care if its right, I wouldn't write it that way and I wouldn't let you write it that way).
(5) is correct. You get an array with five ints.
(6) is correct. A string literal can be used to initialise an array of char. If the array has no size then it gets an extra element for the trailing zero at the end of the string, so you get six chars. char str [5] = "hello"; would also be correct (no trailing zero), char str [4] = "hello"; would be wrong.
Practical tip: Let's say you have an array that should contain 57 constants. You wrote the code but you're not sure if you missed a constant or have one too many. So you declare the array with size 56, compile it and make sure that it fails. Then change the size to 57 compile it and make sure it doesn't fail.
#include <iostream>
using namespace std;
#include <cstdlib>
#include <ctime>
#include <iomanip>
using std::setw;
int main()
{
const int arraySize = 10;
int freq [arraySize] = {0};
srand(time(0));
for (int i = 1; i <=100; i++)
++freq [rand()%10];
cout << "Number" << setw(13) << "Frequency" << endl;
for (int i = 1; i < arraySize; i++)
cout << setw(6) << i << setw(13) << freq[i] << endl;
return 0;
}
const int arraySize = 9;
for (int i = 1; i < arraySize; i++)
Hint:
Code:const int arraySize = 9;
How many digits between 0 and 9 inclusive?
EDIT: You were quick to edit the post.
Next look at this line. What do you expect it to do?
B