saving data

I have a function to find the mode of an array, I want to save this to

Page 1 of 1

6 Replies - 608 Views - Last Post: 14 July 2009 - 04:19 PM Rate Topic: -----

#1 sunal135  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-July 09

saving data

Posted 14 July 2009 - 09:15 AM

I have a function to find the mode of the array( that's wrong too if anyone could help). Where my problem is that I want to save the output from the displaymode function to a file.
When I compile though I get an exe that does nothing
thanks in advance
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>

using namespace std;
//prototyps
int displaymode(ostream&, int[]);
void writeOutput(ostream&,int[]); 

int main()
{
	//creates array
	int data[10] = {5,6,2,5,1,7,5,9,4,3};
	
	int displaymode();
	void writeOutput(); 
	
   
	system("PAUSE");
	return EXIT_SUCCESS;
}
int displaymode(ostream &output, int data[], int data_quantity) 
{
   //find mode
   int mode=0, highest=0;
	
	for (int i = 1; i <= 100; i++)
		if (data[i] > highest)
		{
			highest = data[i];
			mode = i;
		}

	output << "the mode of the array is " << mode << ".\n";
}
//saves the mode in a text file
void writeOutput(ostream &output,int data[], int data_quantity) 
{
	ofstream out2File;		
	//asks for the file to be named
	char saveas[20];
	cout << "name the text file:";
	cin.getline(saveas, 20);
	
	out2File.open(saveas, ios::out);
	
	displaymode(output, data, data_quantity); 
}



Is This A Good Question/Topic? 0
  • +

Replies To: saving data

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: saving data

Posted 14 July 2009 - 09:50 AM

why are you using a char?
void writeOutput(ostream &output,int data[], int data_quantity) 
{
	ofstream out2File;		
	//asks for the file to be named
	string saveas;
	cout << "name the text file:";
	cin>>saveas;
	
	out2File.open(saveas.c_str(), ios::out);
	
	displaymode(output, data, data_quantity); 
}



what exactly are you trying to write, as of now you arnt writing anything

This post has been edited by ImaSexy: 14 July 2009 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 sunal135  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-July 09

Re: saving data

Posted 14 July 2009 - 09:56 AM

I try to save "mode" to a text file.
the saveas char is so I can name the file
Was This Post Helpful? 0
  • +
  • -

#4 brucet  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 13-July 09

Re: saving data

Posted 14 July 2009 - 09:58 AM

Your prototypes are different then your functions.
When you are calling your functions in the main method, you are not supplying them any parameters.
for (int i = 1; i <= 100; i++) should be for (int i = 0; i < 10; i++)
Was This Post Helpful? 0
  • +
  • -

#5 sunal135  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-July 09

Re: saving data

Posted 14 July 2009 - 10:08 AM

#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>

using namespace std;
//prototyps
int displaymode(ostream&, int[]);
void writeOutput(ostream&,int[]); 

int main()
{
	//creates array
	int data[10] = {5,6,2,5,1,7,5,9,4,3};
	
	int displaymode(&output, data[]);
	void writeOutput(&output, data[]);
   
	system("PAUSE");
	return EXIT_SUCCESS;
}
int displaymode(ostream &output, int data[]);

   //find mode
   int mode=0, highest=0;
	
	for (int i = 1; i < 100; i++)
		if (data[i] > highest)
		{
			highest = data[i];
			mode = i;
		}

	output << "the mode of the array is " << mode << ".\n";
}
//saves the mode in a text file
void writeOutput(ostream &output,int data[], int data_quantity) 
{
	ofstream out2File;		
	//asks for the file to be named
	string saveas;
	cout << "name the text file:";
	cin>>saveas;
   
	out2File.open(saveas.c_str(), ios::out);
   
	displaymode(output, data);
}



i declared the functions in main; now though I don't have out put declared.
can I use return???
Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: saving data

Posted 14 July 2009 - 03:45 PM

int displaymode(int data[])
{
	//find mode
	int mode = 0, highest = 0;
   
	for (int i = 0; i < 100; i++)
		if (data[i] > highest) {
			highest = data[i];
			mode = i;
		}

	cout << "the mode of the array is "
		 << mode
		 << "."
		 << endl;
}



prototype for it is int displaymode(int data[]);
call for it is displaymode(data);

prototype you may put before main or in main
not in both places
I think you should pass the size of data array to the function and use it instead a 100 in the loop

This post has been edited by c.user: 14 July 2009 - 03:49 PM

Was This Post Helpful? 0

#7 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: saving data

Posted 14 July 2009 - 04:19 PM

ok, essentially your find mode function is incorrect.
you have the code below (see below).

First error: your for loop tries for 100 iterations to find the highest number which is wrong. your array size is only 10. Change 100 to 10.

Second error: the highest number does not matter when finding the mode. Mode is simply "the number that occurs most frequent in the data/sample set" Your going to need to find a way that keeps track of each number, and how many times each of those number appear (not what you are doing at all)
   //find mode
   int mode=0, highest=0;
   
	for (int i = 1; i < 100; i++)
		if (data[i] > highest)
		{
			highest = data[i];
			mode = i;
		}

	output << "the mode of the array is " << mode << ".\n";



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1