You are here:

C++/arrays and functions


Hello, i have an assignment due for my programming class.  The assignments was to write a program using 2 arrays, one to key in the students answers to a test, and one the held the correct answers to the test.  the next step is to display how many questions the student got right and indicate if they passed or not.  passing is 15 correct answers.  this last part has to be done with a function.  i am not sure how to write a function that will compare the answers in 2 arrays? could you please help ? any help would be greatly appreciated
thank you

Hi, Christopher.

There are several ways to write a function to compare data in two arrays.  The best method is probably to pass each array in as a pointer and a size for the arrays, something like:

   int compareAnswers(int *_array1, int* _array2, int _arraySize)

The body of the function would need to loop from zero to _arraySize and compare the data in each array, something like:

   for (int i = 0; i < _arraySize; ++i)
       if (_array1[i] != _array2[i])
         // do something

That should be enough to get you started.  If you have further questions, please do not hesitate to ask.  I'm here to help. :)


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 All rights reserved.