Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

I got an 89 on my my midterm! Made dumb mistakes :(
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
converting a for loop to a while loop
and then there was one "find correct the errors"
and i didnt see cout << Passed << and didnt put quotations on it haha. oops.
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
the question gave a for loop and asked to convert it to a while loop and i messed up
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
I think lloyddean was asking in what way you messed that up. Did you get the exit condition wrong? The increment location? ...

B
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

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

We didn't get to keep our tests so I'm not sure I can exactly remember. I think I left out I= whatever it was supposed to equal. I only got like 2 points taken off so it wasn't a big deal
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
hi guys im back. your friendly neighbourhood amateur computer programmer.

i need help with this question.


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.


lol i know its probably not hard but arrays make no sense to me.. and i cant tell when soemthing is right/wrong



and


1 Which one is the correct function definition?
(a) void square(int &x)
{
int y ;
y = x*x;
cout << y << endl;
return y;
}

(b) square(int &x)
{
int y = x*x;
cout << y << endl;
return y;
}

(c) int square(int &x)
{
int y ;
y = x*x;
cout << y << endl;
}

(d)
void square (int &x)
{
int y = x*x;
cout << y << endl;
}

could someone tell me what exactly is wrong with the problems that are wrong? :D:D:D:D:D
i think the correct answer is d, because if its void you dont have a return, right?
 
Last edited:

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
If you need help with your homework, read your C++ book, or ask your teacher or a fellow student.
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
If you need help with your homework, read your C++ book, or ask your teacher or a fellow student.

i already explained my prof is extremely difficult to talk to and i dont know anyone in the class, thanks for the advice though mom
 

thundersteele

macrumors 68030
Oct 19, 2011
2,984
9
Switzerland
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?



http://www.cplusplus.com/doc/tutorial/arrays/
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
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?

my book just confused me more, sadly. im trying, i promise. its not that i want you guys to just tell me the answer or anything lol im just confused and want to know the rationale behind some things.


edit:: thanks for the link :)
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
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?
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
The array is defined as having length 4, but is initialized with an array of length 5.

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
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
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/

Explain, no. Perhaps they were writen by a mad man!

None of them modify 'x' and if that really is what was meant should probably be modified to:

Code:
<return-type> square(const int &x)

But would be better written as:

Code:
<return-type> square(int const x)
 

thundersteele

macrumors 68030
Oct 19, 2011
2,984
9
Switzerland
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

Well, if you go back to that link I posted earlier, the chapter after arrays handles character sequences. That should help understand whether or not (6) is correct.
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
im confused whether its right or not, i dont think it is, but icant explain why very well. it just looks wrong in comparison to the examples on that site. for instance

Code:
	char str[] = “hello”;
thats what im talking about btw.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
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.

Those are the ones I thought were right, just dont like guessing blindly by going off other examples, i like to know why. Thanks a lot!!!
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
Code:
#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; 
}

Here is what I'm trying to accomplish with this code:

Counting single digits) Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. Hint: use rand()%10 to generate a random number between 0 and 9. Use an array of ten integers, say frequency, to store the counts for the number of 0’s, 1’s, …, 9’s. Finally, output the frequency for each number.

My problem is I'm getting outputs where the frequency doesn't add up to 100, and also it doesn't display 0. I'm not sure what to do to fix this. Advice

EDIT: False alarm, I figured it out. I set i = 1 instead of 0 because I'm a dummmy.
 
Last edited:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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?

for (int i = 1; i < arraySize; i++)

B
 

oxshannon

macrumors regular
Original poster
Jan 5, 2012
113
0
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

also realized i should have set i = 0. im being careless haha.

i have another problem i have to do where it acts me to input two real numbers , r and θ and return two numbers, x and y. according to x = r cos θ and y = r sin θ. i assume i need to use reference parameters. but i dont understand how to use θ in the program? can i use that as a variable in c++?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.