looping problems

Okay. my function "screenOutput" is going to output to the screen. I need 6 values per line.

In my main() I am to ask the user to input a search value...
call the binary search function and assign the returned value to the integer var, send the array, size of the array, and variable with the search value as arguments (how do i make arguments???)

else- output a message that includes value and pos. at which found
ask user if they would like to search for another value, get input-
continue to loop when user enters 'y'


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

#include <iostream>
#include <fstream>
#include <iomanip>

void readFile(double elements[100] , const int SIZE);
void sortArray(double elements[100], const double SIZE);
void search(double elements[100], const double SIZE);
void screenOutput(double elements[100] , const int SIZE);

using namespace std;

int main()
{
	const int SIZE = 0;
	double elements[100];
	double search;
	char searchAgain;
	int valuePosition;

	//call the first two functions
	readFile(elements, SIZE);
    sortArray(elements, SIZE);
    
    //start the loop
	for(int i = 0; i <= 100; i++)
		cout << "Input a search value..." << endl;
    cin >> search;
	search(i);


    if search < 0; 
    {
    cout << "That value is not in the array" << endl;
    else 
    cout << "value: " << search << "position: " << search(i) << endl;
    }
    
    for (y = "y" && "Y" =
    cout << "Would you like to continue running the program?";
	system("pause");
	return 0;
}

void readFile(double elements[100] , const int SIZE)  
{
	//file stream object - open the file to read
    ifstream assign8File("assign8File.txt");

    //test that the file opened - 
    //output message if failed - pause program - exit w/ return value 1
    if(!assign8File)
	{
		cout << "Error Opening File" << endl;
		system("pause");
		exit(1);
	//loop to read each value in the file into the elements in the array
	else for(int i = 0; i < array_len && assign8File >> *(array+i); i++);
    //close the file    
    assign8File.close();
}

void sortArray(double elements[100], const double SIZE)
{
    bool swap;
	float temp;
	do
	{
		swap = false;
		for(double count = 0; count < (elems - 1); count++)
		{
			if(array[count] > array[count +1])
			{
				temp = array[count];
		        array[count] = array[count +1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	}while(swap);
}

void search(double elements[100], const double SIZE)
{
    double first = 0,
		last = size - 1,
		middle,
		position = -1;
	bool found = false;

	while(!found && first <= last)
	{
		middle = (first + last) / 2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle - 1;
		else
			first = middle + 1;
	}
	return valuePosition;
}

int screenOutput(double elements[100] , const int SIZE)
{
	cout << setw(8) << setprecision(2);
}
on how to pass arrays into function: http://www.cplusplus.com/doc/tutorial/arrays/
if you only want to pass a single element of the array, just do it the way you do normal variables

q: are there typos in you code? or copy and paste?
e.g
1
2
3
//normally it's type arrayName[arraySize], where size is declared constant    
const int SIZE = 0;
double elements[100];

- or lines 26-37: a loop without braces will only affect the first line after loop declaration
- there are also missing braces in readFile(double, const int), and 'array_len' and 'array' are not defined -probably you meant element[] and size?
- there are also undefined variables in sortArray() and search()
- is screenOutput() supposed to return an int?

just follow the link at the top of this post, and from there find the tutorial on functions too
edited for typos 2nd edit: fixed link
Last edited on
alright, i made the changes. Now, in the function screenOutput() I am to set a field width of 8 and set prec. 2. My output needs 6 values of line. I will have my array and the size of it as parameters.

Then, in main() I will start a loop asking user to input search value,

call binary search function and assign the returned value to my integer variable.

Send the array, size of the array, and the variable with the search value as arguments.(but what are arguments!)

: (
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
#include <iostream>
#include <fstream>
#include <iomanip>

void readFile(double elements[100] , const int SIZE);
void sortArray(double elements[100], const double SIZE);
int search(double elements[100], const double SIZE, int valuePosition);
void screenOutput(double elements[100] , const int SIZE);

using namespace std;

int main()
{
	const int SIZE = 0;
	double elements[100];
	double search;
	char searchAgain;
	int valuePosition;

	//call the first two functions
	readFile(elements, SIZE);
    sortArray(elements, SIZE);
    
    //start the loop
	for(int i = 0; i <= 100; i++)
		cout << "Input a search value..." << endl;
    cin >> search;
	search(i);


    if search < 0; 
    {
    cout << "That value is not in the array" << endl;
    else 
    cout << "value: " << search << "position: " << search(i) << endl;
    }
    
    for (y = "y" && "Y" =
    cout << "Would you like to continue running the program?";
	system("pause");
	return 0;
}

void readFile(double elements[100] , const int SIZE)  
{
	//file stream object - open the file to read
    ifstream assign8File("assign8File.txt");

    //test that the file opened - 
    //output message if failed - pause program - exit w/ return value 1
    if(!assign8File)
	{
		cout << "Error Opening File" << endl;
		system("pause");
		exit(1);
	//loop to read each value in the file into the elements in the array
	else for(int i = 0; i < elements[] && assign8File >> *(SIZE+i); i++);
    //close the file    
    assign8File.close();
}

void sortArray(double elements[100], const double SIZE)
{
    bool swap;
	float temp;
	do
	{
		swap = false;
		for(double count = 0; count < (SIZE - 1); count++)
		{
			if(elements[count] > elements[count +1])
			{
				temp = elements[count];
		        elements[count] = elements[count +1];
				elements[count + 1] = temp;
				swap = true;
			}
		}
	}while(swap);
}

int search(double elements[100], const double SIZE, int valuePosition)
{
    double first = 0,  //first array element
		last = SIZE - 1, //last array element
		middle,  //midpoint of search
		position = -1;  //position of search value
	bool found = false;  //flag

	while(!found && first <= last)
	{
		middle = (first + last) / 2;  //calculate midpoint
		if (elements[middle] == valuePosition)  //if value is found at midpoint
		{
			found = true;
			position = middle;
		}
		else if (elements[middle] > valuePosition)  //if value is in lower half
			last = middle - 1;
		else
			first = middle + 1;  //if value is in upper half
	}
	return valuePosition;
}

int screenOutput(double elements[100] , const int SIZE)
{
	cout << setw(8) << setprecision(2);
}

ooh, I can play "post the same reply twice, too" :p

on how to pass arrays into function: http://www.cplusplus.com/doc/tutorial/arrays/
if you only want to pass a single element of the array, just do it the way you do normal variables

q: are there typos in you code? or copy and paste?
e.g
1
2
3
//normally it's type arrayName[arraySize], where size is declared constant    
const int SIZE = 0;
double elements[100];



- or lines 26-37: a loop without braces will only affect the first line after loop declaration
- there are also missing braces in readFile(double, const int), and 'array_len' and 'array' are not defined -probably you meant element[] and size?
- there are also undefined variables in sortArray() and search()
- is screenOutput() supposed to return an int?

just follow the link at the top of this post, and from there find the tutorial on functions too
p/s sorry I messed up the link earlier >:D
Topic archived. No new replies allowed.