3 Replies - 748 Views - Last Post: 26 March 2010 - 09:38 PM Rate Topic: -----

#1 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Reading and Printing Array. Help!

Posted 26 March 2010 - 03:42 PM

I have done 95% of the program. Then first part of the program was to generate an array and sort to the console after the user has entered a negative number. Now the second part if the user enters a number less than the previous one the program should stop and print the numbers. How do I do that any ideas.
I thought of assigning the value of the array to the first element , then compare that to the next number, however dont know I to translate that language into code. Thanks.

#include <iostream>

int Function1(double []);
void PrintArray(const double [], int);

const int MAXSIZE =100;
using namespace std;

int main()
{
	double array1[100];
	int count;

	count = Function1(array1);
	cout<<"There were "<<count<<" numbers entered into the array."<<endl;


	//function call to function that will print the array
	PrintArray(array1, count);


	return 0;
}

//Write the function here to read in numbers into the array.  The array is called myarray.
//add comments describing what this function does, what is passed to it, what it sends back, etc.
int Function1(double myarray[])
{
	int i =0, number;

	do {
		cout << "Enter the number:";
		cin >> number;

		if ( number > 0 && (myarray[i] = MAXSIZE))	{
			myarray[i] = number;
			i++;
		}
	} while ( (i<100 && number>0) && myarray[i] );

	return i;
}

//Write the new function here along with comments
//add comments describing what this function does, what is passed to it, what it sends back, etc.
void PrintArray(const double a1[], int size)
{
	for ( int index = 0; index < size; index ++)
	{
		cout << "The number number entered is" << a1[index] << endl;
	}

}
int BinarySearch(double a[ ], int size, double key)
{
	int high = size -1, low = 0, middle;
	while( low <=high)
	{
		middle = ( low + high)/2;
		if ( key == a[middle])
		{
			return middle;
		}
		else if ( key < a[middle])
		{
			high = middle -1;
		}
		else
		{
			low = middle + 1;
		}

	}
	return -1;
}








This post has been edited by Martyn.Rae: 26 March 2010 - 10:39 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Reading and Printing Array. Help!

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Reading and Printing Array. Help!

Posted 26 March 2010 - 04:04 PM

hello dinisrainman

welcome to </D.I.C.> and thanks for your post.

A couple of things, please use the code tags around your code, I think you may have tried judging by the [\ in the first line. use the code tags ( <> ) above where you are typing in.

I'd like to recommend a good tutorial on functional decomposition as your problem starts from your statement -

Quote

however dont know I to translate that language into code
.

When you have had a read, lets continue this work towards your goal.
Was This Post Helpful? 1
  • +
  • -

#3 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Re: Reading and Printing Array. Help!

Posted 26 March 2010 - 04:12 PM

View PostgregoryH, on 26 March 2010 - 03:04 PM, said:

hello dinisrainman

welcome to </D.I.C.> and thanks for your post.

A couple of things, please use the code tags around your code, I think you may have tried judging by the [\ in the first line. use the code tags ( <> ) above where you are typing in.

I'd like to recommend a good tutorial on functional decomposition as your problem starts from your statement -

Quote

however dont know I to translate that language into code
.

When you have had a read, lets continue this work towards your goal.


he GregoryH I promise I'll post with tags like that. I'm a returning member, but the tutorial you directed me to doesn't explain much. Do you have any "array sorting or inserting algorithms" tutorial?
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Reading and Printing Array. Help!

Posted 26 March 2010 - 09:38 PM

Suppose that you have a counter ("size"), to keep track of elements in the array. This counter should be initially set to 0, because arrays in C/C++ *should* be indexed from 0 -> N - 1. Now, in the do-while loop, you have to compare:
// your_array is your array
// obviously if this condition is met, then 
// you have entered a number less than the
// previous one
if (your_array[size - 1] > a[size]) {
   handle_err_mesage; exit loop;
}


This post has been edited by sarmanu: 26 March 2010 - 09:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1