UINT_MAX of array probelm? Functions

closed account (N7960pDG)
I need Help. I am really confused on how to do the below Function and have been working on it for awhile now trying to do it but now I just have my code all mixed up for the last function. Any ideas on how to do the find function below:

unsigned find( unsigned sought, const unsigned a[], unsigned els );
If the value sought appears in the array, find returns the index of the first element whose value is sought. If not, find returns UINT_MAX.
For example, find( 5, {2, 6, 5, 3, 5, 10, 5}, 7 ) returns 2.
For example, find( 5, {1, 2, 3, 4}, 4 ) returns UINT_MAX.

Thanks in advance! Everything else works, I just need help on last function 6.


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <limits>

using namespace std;

bool die(const string & msg);

//FUNCTIONS PROTOTYPE VOIDS
	void input( unsigned a[], unsigned els );
	void output( const unsigned a[], unsigned els );
//FUNCTIONS PROTOTYPE RETURNS	
	unsigned sum( const unsigned a[], unsigned els );
	double averageOdds( const unsigned a[], unsigned els );
	void square( unsigned a[], unsigned els );
	unsigned find( unsigned sought, const unsigned a[], unsigned els );

int main(){
	unsigned a[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
	unsigned sought;
	input(a, 5);

	output(a, 5);

	cout<<"Sum is " <<sum(a,5) <<endl;

	cout<<"Average of odds " <<averageOdds(a,5) <<endl;

	cout<< "Square array is: "; square(a,5); cout<<endl;

	cout<<"Find value: "; 
	find(sought,a,5); cout<<endl;
}//endMain

//DIE FUNCTION
bool die(const string & msg){
	cout<<"Fatal Error: " <<msg <<endl;
	exit(EXIT_FAILURE);
}

//Functions
void input( unsigned a[], unsigned els ){
	for( unsigned i = 0; i < els; i++ ){
		cout<<"[" <<i <<"/" <<els <<"]: ";
		cin >>a[i] || die("input failure");
	}
}//1....................................................

void output( const unsigned a[], unsigned els ){
	cout<<"[ " <<els <<" ]" <<"{ ";
	for(unsigned i = 0; i < els; i++){
		if(i > 0)
			cout<<" ";
		cout<<a[i] <<",";
	}
	cout<<" }" <<endl;
}//2.....................................................

unsigned sum( const unsigned a[], unsigned els ){
	double ret = 0;
	for (unsigned i = 0; i < els; i++)
		ret += a[i];
	return ret;

}//3.........................................................

double averageOdds( const unsigned a[], unsigned els ){
	double avg = 0;
	double sum1 = 0;
	double odd = 0;
	double even = 0;
	
	for (unsigned i = 0; i < els; i++){
		if ((a[i] % 2 != 0)){
			odd += a[i];
			avg = odd/els;
		}
		else
		{
			if(odd == 0  ) die("no element");
		}
	}
	return avg;
}//4.............................................................

void square( unsigned a[], unsigned els ){
	cout<<"[ " <<els <<" ]" <<"{ ";
	for(unsigned i = 0; i < els; i++){
		if(i > 0)
			cout<<" ";
		cout<<a[i] * a[i] <<","; 
	}
	cout<<" }" <<endl;
}//5..............................................................

unsigned find( unsigned sought, const unsigned a[], unsigned els ){
	cout<< "find( ";
	 sought = 0;
	for(unsigned i = 0; i < els; i++){
		cin >>sought || die("input failure");
		if(i > 0){
			cout<<" ";
		cout<<sought <<"{ " <<a[i] <<",";
	//UINT_MAX = a[i];
	cout<<" }" <<endl;
		}
	 sought = a[i];
	 if (sought != a[i]){
		size_t i = UINT_MAX;
			cout<< a[i]; 
	 }
	 else{sought = a[i];
	 }

	//if (findnum == a[i])
	//	findnum = a[i];
	//else
	//{
	//	//a[i] = findnum;
	//	findnum = UINT_MAX;
	//	cout<< findnum <<"Uint_Max" <<endl;
	//}
	//

	}
	return sought;
}//6 
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
#include <iostream>
#include <climits> // UINT_MAX

unsigned int find( unsigned int sought, const unsigned int arr[], unsigned int arr_sz ) ;

int main() {

    const unsigned int ARRAY_SIZE = 6 ;
    const unsigned int a[ARRAY_SIZE] = { 11, 22, 33, 44, 33, 55 };
                          // positions:   0   1   2   3   4   5

	unsigned int sought = 11 ;
	std::cout << "Find value " << sought << ": " << find( sought, a, ARRAY_SIZE ) << '\n' ;

	sought = 33 ;
	std::cout << "Find value " << sought << ": " << find( sought, a, ARRAY_SIZE ) << '\n' ;

	sought = 55 ;
	std::cout << "Find value " << sought << ": " << find( sought, a, ARRAY_SIZE ) << '\n' ;

	sought = 92 ;
	std::cout << "Find value " << sought << ": " << find( sought, a, ARRAY_SIZE ) << '\n' ;
}

unsigned int find( unsigned int sought, const unsigned int arr[], unsigned int arr_sz )
{
    for( unsigned int pos = 0 ; pos < arr_sz ; ++pos ) {
        if( arr[pos] == sought ) return pos ; // found at position pos
    }

    return UINT_MAX ; // not found
}

http://coliru.stacked-crooked.com/a/90e6fa1e92e1426d
closed account (N7960pDG)
Thanks! That helped me a little but I am trying to return exactly what it ask for in my first description. This is what code I have now and it is giving me the sought number asked for, but it is also making my 1st array disappear, and not exactly giving me what I am looking for. I have tried to code this for the past 5 hours and have no clue where to go. Can someone help me? Thanks in advance again.

This is what output I need.
unsigned find( unsigned sought, const unsigned a[], unsigned els ); //Function6
If the value sought appears in the array, find returns the index of the first element whose value is sought. If not, find returns UINT_MAX.
For example, find( 5, {2, 6, 5, 3, 5, 10, 5}, 7 ) returns 2.
For example, find( 5, {1, 2, 3, 4}, 4 ) returns UINT_MAX.

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
int main(){
	unsigned a[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
	unsigned sought = 0;

	input(a, 5);

	output(a, 5);

	cout<<"Sum is " <<sum(a,5) <<endl;

	cout<<"Average of odds " <<averageOdds(a,5) <<endl;

	cout<< "Square array is: "; square(a,5); cout<<endl;
//the above are linked to my other functions, pay no mind. 

	cout<<"Find value: "; 
	find(sought,a,5); cout<<endl;   //function 6
}//endMain


unsigned find( unsigned sought, const unsigned a[], unsigned els ){ 
	unsigned soughtNum = 0;
	
	for(;;){
		 if (!(cin >>soughtNum || die("input failure"))){
		 cout<<"Sought Number: " <<soughtNum;
		 break;
		}
		 cout<<"[ " <<els <<" ]";
		for(unsigned i = 0; i < els; i++){
			if(i > 0){
			cout<<" " <<"{ " <<a[i] <<",";
			}
		cout<<" }" ;
		if (a[i] == soughtNum)
			cout<< " " <<soughtNum 
				 <<" " <<"{ " <<a[i] <<"," <<" }" ;
	}
	}
	return soughtNum;
}//6 
What do you mean by sought? I'm kind of confused by what you're asking for. If the number is the same as sought, it returns how many times it shows up?
@TC: Does JLBorges's version of the function not do what you want? Your code seems very complex for the simple requirements you have given. Why, for example, are you reading input in your find() function? You are not asked nor required to do that by the requirements.
Mina123 wrote:
I am trying to return exactly what it ask for in my first description.

The JLBorges' version does exactly that (and nothing more).

Personally, I find the similarity of value and index -types disturbing. Thus, for educational clarity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <limits>

using Valuetype = unsigned int; // if the C++11 'using alias = type;' syntax is new
using Indextype = unsigned int; // then read about it

Indextype find( Valuetype sought, const Valuetype arr[], Indextype arr_sz )
{
    for ( Indextype pos = 0 ; pos < arr_sz ; ++pos ) {
        if ( arr[pos] == sought ) return pos ; // found at position pos
    }
    // loop has completed, so no element equals sought

    // Check reference documentation to see whether this equals UINT_MAX
    return std::numeric_limits<Indextype>::max();
}
Topic archived. No new replies allowed.