Question:
Array and function: Square of numbers between the user's numbers input.?
Zulaika Azmi
2012-12-27 10:49:14 UTC
Write a program based on this sample output:

Please enter first number : 2
Please enter second number : 9
The squares for all numbers between 2 and 9 are : 4 9 16 25 36 49 64 81

Use function findSquare() to square all the numbers and display the result.
Four answers:
cja
2012-12-28 06:39:31 UTC
You always need to pay attention to what your compiler tells you, and it's a great way to learn. You need to work on fixing these errors:



loopTst.cpp: In function `int main()':

loopTst.cpp:17: error: invalid conversion from `int*' to `int'

loopTst.cpp:4: error: too few arguments to function `void findSquare(int, int)'

loopTst.cpp:17: error: at this point in file

loopTst.cpp: In function `void findSquare(int*)':

loopTst.cpp:25: error: expected primary-expression before ';' token

loopTst.cpp:25: error: `b' undeclared (first use this function)

loopTst.cpp:25: error: (Each undeclared identifier is reported only once for each function it appears in.)

loopTst.cpp:27: error: `j' undeclared (first use this function)

loopTst.cpp:28: error: expected `;' before '}' token

loopTst.cpp:31: error: `a' undeclared (first use this function)

loopTst.cpp:31: error: `mySquare' undeclared (first use this function)

loopTst.cpp:31: error: name lookup of `i' changed for new ISO `for' scoping

loopTst.cpp:25: error: using obsolete binding at `i'

loopTst.cpp:29: warning: unused variable 'newArray'



Unless your assignment explicitly requires you to use an array for this problem, don't; there's no need for an array. Try to explain to yourself what every part of your code is doing. In many cases you should be telling yourself, "that makes no sense!". The simple fix for your code is this:



#include



using namespace std;



void findSquare(int, int);



int main(int argc, char *argv[]) {

  int a, b;



  cout << "Please enter the first number : ";

  cin >> a;

  cout << "Please enter the second number : ";

  cin >> b;

  findSquare(a, b);

  return 0;

}



void findSquare(int a, int b) {

  for (int i = a; i <= b; i++ ) {

    cout << i*i << " ";

  }

  cout << endl;

}



#if 0



Sample run:



Please enter the first number : 2

Please enter the second number : 9

4 9 16 25 36 49 64 81



#endif
?
2012-12-27 22:49:45 UTC
You need to fill the array during the iteration (inside the for loop). You are trying to magically say j is the set of elements of a new array you just created. j is just a number that changes every journey through the for loop. Plus, you might have scope issues, that, and I noticed a few typos, nothing that can't be easily fixed. You are on the right track though. Also, where did mySquare[i] come from? This suspiciously looks like a "find the errors" type of homework question. When I see those, I just switch languages to prove a point (yeah, I fail the homework, but so what ;-)...



print "Please enter two integers separated by a space: "

a, b = gets.strip.split.map &:to_i

puts (a..b).map {|i| i**2}
Anonymous#439734
2012-12-27 19:31:03 UTC
Before I start, let me say I'm not expert and a learning student myself. I also need to say that I'm learning Java, and not C++. I know nothing of arrays of the moment.



If you can't figure a program out, a good way to tackle it is to take 15-30 minutes (or however long you may need) and plan the program bit by bit and write out some pseudo code, or instructions that will be easily (hopefully) translated into code. I've written some pseudo code for you, and I hope it helps.



[[Use some sort of input command for both numbers:



Please enter... etc.



Store in these variables:



numberOne

numberTwo



counter = numberOne //for readability]



[Make a loop:



while counter < numberTwo



square number in counter

store the square into array



increase counter by 1]



[Print output:



The squares for all numbers between... etc.]]



A couple of notes:

I see you're using a for loop; if it works better, use it (I haven't learned about those in Java yet).

I did take some time to write this, but it may not be perfect.



Lastly, I know this is all pretty small, but I hope it helps if even a little! Good luck!
Ari
2012-12-27 20:47:22 UTC
no need for an array at all. just prompt for the numbers, use a for loop and print as you go.



#include



using namespace std;



void findSquares(int start, int end);



int main( ) {



int start, end;



cout<<"Enter first number: ";

cin>>start;



cout<<"Enter second number: ";

cin>>end;



findSquares(start, end);



return 0;

}



void findSquares(int start, int end) {

//print the message

cout<<"The squares of all of the numbers from "<


//run a for loop from start to end. find the square and print it out.

for(int i=start; i<=end; i++) {

int square = i * i;

cout<
}

//print a new line when you're done

cout<
}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...