Arrays and corresponding elements

#include <stdio.h>
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::setw;
const int SIZE=10;
int i=0;
int j=0;
bool test (int, int);//func. prototype
int main(){
int A[10]={1,4,4,4,5,2,6,7,3,2};
int B[10]={1,4,4,4,5,2,6,7,5,2};
/*bool test(int A, int B);*/
cout<<test(A[10], B[10]);[/b]//how to convert from boolean to char?
static_cast<char>(test(A[10], B[10]));
cin>>i;
return 0;
}

bool test (int i, int j){
int A[10];
int B[10];
while (i<10 && j<10){
i++;
j++;
if (A[i]=B[j])
return true;}
//for (int i=1; A[i]!='\0'; i++ ){
// for (int j=1; j=i; j++ ){
// if (A[i]=B[j])
// /*cout<<"true";*/
// return true;
// for (int i=0; i<=10;i++){
// for (int j=0; i=j; j++){

// if (A[i]!=B[j])
// /*cout<<"False";*/
// return false;}}
}

The warning message I'm getting: Run-Time Check Failure #3 - The variable 'B' is being used without being initialized. @line cout<<test(A[10], B[10]);
Why?
The same for A.
Hope this helps, or a least points you in a direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE = 10;

bool test(int, int);

int main(){

	int a[SIZE] = { 1, 4, 4, 4, 5, 2, 6, 7, 3, 2 };
	int b[SIZE] = { 1, 4, 4, 3, 5, 2, 6, 7, 5, 2 };

	for (int i = 0; i < SIZE; i++)
		cout << test(a[i], b[i]) << endl;

	return 0;
}

bool test(int a, int b){
	return (a == b);
}
Last edited on
Please use use the code commands when posting and re-input your code for easier reading.
I have done it for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::setw;

const int SIZE=10;
int i=0;
int j=0;

bool test (int, int);//func. prototype

int main()
{
int A[10]={1,4,4,4,5,2,6,7,3,2};
int B[10]={1,4,4,4,5,2,6,7,5,2};

/*bool test(int A, int B);*/
cout<<test(A[10], B[10]);[/b]//how to convert from boolean to char?
static_cast<char>(test(A[10], B[10]));
cin>>i;
return 0;
}

bool test (int i, int j)
{
int A[10];
int B[10];
while (i<10 && j<10){
i++;
j++;
if (A[i]=B[j])
return true;}
//for (int i=1; A[i]!='\0'; i++ ){
// for (int j=1; j=i; j++ ){
// if (A[i]=B[j])
// /*cout<<"true";*/
// return true;
// for (int i=0; i<=10;i++){
// for (int j=0; i=j; j++){

// if (A[i]!=B[j])
// /*cout<<"False";*/
// return false;}}
}


A couple side notes before getting to your question... I notice that you have a few headers from C. You can replace every one of the using std:: headers in your program by implementing:
using namespace std

You already have #include <iomanip> , why do you have using std::setw?

Just a couple small things that will make your code a little better ;)

Now, to your question. Have you tried calling the function outside of your cout statement? This could be an issue, although maybe not your problem..

I do see a problem with your bool test function though:
You are returning true as soon as a value matches, this is going to immediately break your loop. If you are trying to compare every value of the 2 arrays, this won't work. Think about maybe making a flag variable that you can set to true or false, that can change if the values are or are not equal to one another

also,
Topic archived. No new replies allowed.