• 0

What to do? Teacher is doing bad practice


Question

Okay well I seen my teacher is like trying to set us up for failure.. Look at these codes and the questions she asks..

#include <iostream>

using namespace std;

void prod ( int, int, int& );

void main ()
{
int a =1, b =2, c =1;
prod(a,b,c);
prod(c,b,a);
prod(c,a,b);
}

void prod ( int w, int x, int& y )
{
y = w * x;
}

Like what the hell is that? It returns nothing and thats how she put it and asked what is the final value of a, b, & c..

Another code:

for ( int i = 0; i < 10; i++ )
{
scores[i] = i * i;
happy[i+10] = scores[i];
}

Then I am suppose to figure out scores[0], scores[9], scores[10], happy[0], & happy[19].

Last code is

index = -1;
mystery = 50;
for ( int i =0; i < 10; i++ )
{
if ( scores[i] == mystery )
{
index = i;
cout << index << endl;
}
}

Then she asks what is displayed, what if mystery equals 55, & what happens when it is if ( scores = mystery ).

Now what the heck am I suppose to do with those bits?! They are missing tons, but she wants me to figure out these questions (and no I am not suppose to correct the code or use Visual Studio).

Any help or maybe I am going to far into this??

Also I need to make a program that calculates the square of a number, the cube of a number, add 1 to the number, subtract 1 from the number, and provide option to choose..

Any guidance on that one will be appreciated as well!

Link to comment
Share on other sites

Recommended Posts

  • 0

Its to get me prepared for the programming class learning the planning stuff first

Link to comment
Share on other sites

  • 0

Okay with all the help from yall I figured all of them out except the one with the mystery bit in it.. I can not seem to figure out what its trying to do. To me nothing would be displayed until I make the if statement an assignment.

Am I messing something here?

This is the code I edited to seem to fit what she is meaning

void main()
{

	for ( int i = 0; i < 10; i++ )
	{
	int scores[10];
	int index = -1;
	int mystery = 50;
	scores[i] = i * i;

		if ( scores[i] == mystery )
		{
			index = i;
			cout << "Index: " << index << endl;
		}
	}
}

Not asking for the answers, but am I missing something that the top does not return anything?

Link to comment
Share on other sites

  • 0

Okay with all the help from yall I figured all of them out except the one with the mystery bit in it.. I can not seem to figure out what its trying to do. To me nothing would be displayed until I make the if statement an assignment.

Am I messing something here?

This is the code I edited to seem to fit what she is meaning

Well, your editing isn't quite right. The code she gave you is purposefully not a complete program. Looking at the code she gave you I would assume that scores is declared and filled with values before you get to the code snippet she gave. Your editing redeclares scores as an empty array every time the for loop loops.

So if scores is filled with values, the if statement will compare the value in scores at index i with mystery, if they are equal then it will print something out.

Here's the code with some concrete values for scores. Once you understand the mechanics you should be able to describe what it does without knowing the actual values.

int main() {
   int scores [] = {65, 45, 89, 92, 73, 32, 50, 52, 78, 50};
   index = -1;
   mystery = 50;
   for ( int i =0; i < 10; i++ )
   {
      if ( scores[i] == mystery )
      {
         index = i;
         cout << index << endl;
      }
   }
}

Link to comment
Share on other sites

  • 0

So from what you said and your example I should most likely include my results from my other code?

Link to comment
Share on other sites

  • 0

Your teacher is not "doing bad practice" (except for writing "void main", that is). This is all very simple C++ code that any intermediate to advanced programmer can understand without much effort.

Let's look at those samples:

#include <iostream>

using namespace std;

void prod ( int, int, int& );

void main ()
{
    int a =1, b =2, c =1;
    prod(a,b,c);
    prod(c,b,a);
    prod(c,a,b);
}

void prod ( int w, int x, int& y )
{
    y = w * x;
}

The key thing to understand here is the difference between passing by value and passing by reference. The third argument to prod is passed by reference (with the & sign), which means the original variable will be modified, enabling it to act as a return value. If you're not sure what a reference in C++ is, read your textbook again. Or the C++ FAQ.

***

for ( int i = 0; i < 10; i++ )
{
    scores[i] = i * i;
    happy[i+10] = scores[i];
}

You can assume scores is an array of ints of at least size 10, and happy is another array of ints at least size 20. This is a for-loop, where i gets incremented from 0 to 9, and you store values calculated with i into the two arrays at specific locations. For example, when i is 2, the body of loop will do:

scores[2] = 2 * 2; // now scores[2] is 4
happy[12] = scores[2]; // now happy[12] is the same as scores[2], which is 4

***

index = -1;
mystery = 50;
for ( int i =0; i < 10; i++ )
{
    if ( scores[i] == mystery )
    {
        index = i;
        cout << index << endl;
    }
}

You can assume index and mystery are ints, and scores is the same int[] as last time. Since now you know the values in it, you can tell what will happen on each loop iteration.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.