Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given an array of integers, return true if 6 appears as either the first or last element in the array.

The array will be size 1 or more.

You don't know the array size. The program is supposed to work with an array of any size.

To find array size:

int size = sizeof(array) / sizeof( array[0] )
Function prototype:

bool firstLast6(int array[ ], int size);

Sample runs:
int array1 [ ] = {1, 2, 6};

int array2 [ ] = {6, 1, 2, 3};

int array3 [ ] = {13, 6, 1, 2, 3};

firstLast6(array1, size) → returns true

firstLast6(array2, size) → returns true

firstLast6(array3, size) → returns false

How to show 'true' , 'false' instead of '1' and '0':

What I have tried:

C++
#include <iostream>
using namespace std;

bool firstLast6(int array[ ], int size);

int main()
{
	int array1 [ ] = {1, 2, 6};
	int array2 [ ] = {6, 1, 2, 3};
	int array3 [ ] = {13, 6, 1, 2, 3};

	int size =  sizeof(array) / sizeof( array[0] )

}
Posted
Updated 7-May-16 19:57pm
v5
Comments
AnvilRanger 7-May-16 17:36pm    
You better get to writing then. Good luck and if you have specific questions about some code you have written please come back.
Sergey Alexandrovich Kryukov 7-May-16 17:36pm    
You don't need a program. You need to write a program. Can you see the difference?

And you did not try anything.
No answer can help you. If you get receive some code, it will take out your chance to lean anything on this assignment.
It's amazing that you did not even try to detect this 6, because the problem is extremely simple. Did you understand the assignment?

—SA
Member 12509125 7-May-16 17:56pm    
can please give me an example because I don't understand how this works
Sergey Alexandrovich Kryukov 7-May-16 18:09pm    
No, please explain what exactly you don't understand. People will spend some time on your questions only if they see you are qualified enough to understand the answers. Most likely, you simply need to start reading some book/manual on C/C++ and general programming, and read it from the very beginning. Anything else hardly can help you; I think so because I can see you inadequate response to the assignment.
—SA
Patrice T 7-May-16 18:10pm    
So you already managed to copy lines of code from problem statement.
start working !

1 solution

This is pretty trivial - but it's your homework, so I'm not going to do it for you.
The code you have shown is close to what the question tells you to use, but not right: it doesn't refer to any of the arrays you declare, so it won't return the actual size of any of them. To do that, you would need:
C#
int array1 [ ] = {1, 2, 6};
int array2 [ ] = {6, 1, 2, 3};
int array3 [ ] = {13, 6, 1, 2, 3};

int sizeOfArray1 =  sizeof(array1) / sizeof(array3[0]);
int sizeOfArray2 =  sizeof(array2) / sizeof(array3[0]);
int sizeOfArray3 =  sizeof(array3) / sizeof(array3[0]);

But the question specifically wants you to create a function to accept any array:
C#
bool firstLast6(int array[ ], int size);
and then call it passing each array and its size in turn:
C#
... firstLast6(array1, sizeOfArray1) ...
... firstLast6(array2, sizeOfArray2) ...
... firstLast6(array3, sizeOfArray3) ...
I'm being careful here not to give you the solution, you notice: you need to do something with this code to use the result it returns.
So create the function (with dummy content if necessary), set up the main function to call it and test that it works.
Then write the real content for the function and test that. The question is pretty explicit about what you have to do and does give you everything you need to know to complete this.
Give it a try!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900