I have wrote the program and it worked fine until I had to include one more array into it. As you can see below the two arrays of volume and mass are working properly now I need to include a third array which reads in the sample name that coincides with the mass and the volume. I have tried the getline function and that didnt work. This is my first class in computer programming and im very new to it so if i have made obviouse mistakes i apologize. I tried as much as i can myself and Im not exactly sure how to proceed. Please help
Here is the program: -
# include <string>
-
# include <iostream>
-
# include <fstream>
-
using namespace std;
-
void readinput (double[], double[], string, int, ifstream &infile);
-
void printvalues (double[], double[], string, int, ofstream &outfile);
-
void linearsort (double[], double[], string, int);
-
void bubblesort (double[], double[], string, int);
-
int line_search (double[], double, int);
-
int main()
-
{
-
ofstream outfile ("prog7.txt");
-
ifstream infile ("assignment7.txt");
-
int n, position;
-
if (infile.bad())
-
{
-
cout<<"unable to read from file";
-
exit(1);
-
}
-
infile>>n;
-
double mass[n], volume[n], newnumber;
-
string name;
-
readinput(mass, volume, name, n, infile);
-
cout<<name[0]<<name[1]<<name[2]<<name[3]<<name[4]<<name[5];
-
printvalues(mass, volume, name, n, outfile);
-
linearsort(mass, volume, name, n);
-
printvalues(mass, volume, name, n, outfile);
-
bubblesort(volume, mass, name, n);
-
printvalues(mass, volume, name, n, outfile);
-
while(infile>>newnumber)
-
{
-
position=line_search(mass, newnumber, n);
-
if (position == -1)
-
{
-
cout<<"The number "<<newnumber
-
<<" does not occure in the mass array"<<endl;
-
}
-
else
-
{
-
cout<<newnumber<<" is found at position "<<position
-
<<". The volume of that sample is "<<volume[position]<<endl;
-
}
-
position=line_search(volume, newnumber, n);
-
if (position == -1)
-
{
-
cout<<"The number "<<newnumber
-
<<" does not occure in the volume array"<<endl;
-
}
-
else
-
{
-
cout<<newnumber<<" is found at position "<<position
-
<<". The mass of that sample is "<<mass[position]<<endl;
-
}
-
cout<<endl;
-
}
-
infile.close();
-
outfile.close();
-
getchar();
-
return 0;
-
}
-
-
-
//Function readinput:
-
//Input: 2 arrays, int n, and infile
-
//Process: Read the data values from the input values into 2 arrays.
-
void readinput (double a[], double b[], string c[], int n, ifstream &infile)
-
{
-
for (int i=0; i<n; i++)
-
infile>>a[i]>>b[i]>>c[i];
-
return;
-
}//read input
-
-
-
-
//Function bubblesort:
-
//Input: 2 arrays
-
//Process: Sorts the arrays in ascending order of volume
-
void bubblesort (double a[], double b[], string c[], int n)
-
{
-
double temp, temp2;
-
string temp3;
-
bool swapped;
-
do
-
{
-
swapped = false;
-
for (int i=0; i<n-1; i++)
-
if (a[i] > a[i+1])
-
{
-
temp = a[i];
-
temp2 = b[i];
-
temp3 = c[i];
-
a[i] = a[i+1];
-
b[i] = b[i+1];
-
c[i] = c[i+1];
-
a[i+1] = temp;
-
b[i+1] = temp2;
-
c[i+1] = temp3;
-
swapped = true;
-
}
-
}
-
while (swapped);
-
return;
-
}//bubblesort
-
-
-
//Function linearsort:
-
//Input: 2 arrays
-
//Process: Sorts the arrays in descending order of mass
-
void linearsort (double a[], double b[], string c[], int n)
-
{
-
double temp, temp2;
-
string temp3;
-
for (int i=0; i<n-1; i++)
-
for (int j=i+1; j<n; j++)
-
if (a[i] < a[j])
-
{
-
temp = a[i];
-
temp2 = b[i];
-
temp3 = c[i];
-
a[i] = a[j];
-
b[i] = b[j];
-
c[i] = c[j];
-
a[j] = temp;
-
b[j] = temp2;
-
c[j] = temp3;
-
}
-
return;
-
}// linearsort
-
-
-
//Function linsearch:
-
//Input: double array, number to be searched for and n
-
//Process: Determines whether or not the input number occurs in the array
-
//Output: The position of that number or -1 if it cannot be found
-
int line_search (double a[], double newnumber, int n)
-
{
-
for (int position=0; position<n; position++)
-
if (a[position] == newnumber)
-
return position;
-
return -1;
-
}//line_search
-
-
-
//Function printvalues:
-
//Input: 2 arrays, int n and outfile
-
//Process: To print the arrays into a table.
-
void printvalues (double a[], double b[], string c[], int n, ofstream &outfile)
-
{
-
outfile<<" Collected Data"<<endl;
-
outfile<<"Mineral Mass Volume"<<endl;
-
for (int i=0; i<n; i++)
-
{
-
outfile.setf(ios_base::fixed, ios_base::floatfield);
-
outfile.precision(2);
-
outfile.setf(ios::left);
-
outfile.width(12);
-
outfile<<c[i]<<a[i]<<b[i]<<endl;
-
}
-
outfile<<endl<<endl;
-
return;
-
}//printvalues
3 2638 Banfa 9,065
Recognized Expert Moderator Expert
I think you problem is that using you syntax
string name;
should be
string name[n];
In fact you have notably left out the array specifiers [] in a lot of places where you are passing the name parameter.
Additionally the code
double mass[n];
doesn't compile for me as standard C++ does not support dynamically sized arrays (not sure which version you are using though).
That was absolutely correct I can't believe I missed that. It worked out perfectly , Im using Dev-C++ and double mass[n] workes with no problem. I just got confused how i was suppose to declare an array of words, I wasnt sure you can have an array or words and not just characters.
Thank you very much
Banfa 9,065
Recognized Expert Moderator Expert
Im using Dev-C++ and double mass[n] workes with no problem.
That is fine but you had better be sure that your tutor/lecturer is too (assuming you have one.
...
Now on a point of style you have code similar to the following -
temp = a[i];
-
temp2 = b[i];
-
temp3 = c[i];
-
a[i] = a[i+1];
-
b[i] = b[i+1];
-
c[i] = c[i+1];
-
a[i+1] = temp;
-
b[i+1] = temp2;
-
c[i+1] = temp3;
-
In both of your sort routines. Now this code works (which is why it is a style point and not a logic point) but this is not very elegant, this code logical performs
For array a swop the values located at i and i+1
For array b swop the values located at i and i+1
For array c swop the values located at i and i+1
which you have written long handed. To neaten it you could easily write Swop functions for the 2 types of variable use (double and string) something with the prototypes
void Swop(double &op1, double &op2);
void Swop(string &op1, string &op2);
these functions would probably embody the code you already have (i.e. use a temporary variable) and then the code becomes -
Swop(a[i], a[i+1]);
-
Swop(b[i], b[i+1]);
-
Swop(c[i], c[i+1]);
-
which looks much better and says what is happening.
If you really wanted to get snazzy rather than write 2 functions you could write a single template function
template <class T>
void Swop(T &op1, T &op2);
but that may be a little bit complex for a beginner.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: ???? |
last post by:
this is an excerpt from C++ Primer 3rd edition by Lippman, Lajoie.
How might we read an unknown number of input values? At the end of
Section 1.2, we did just that. The
code sequence
string word;
while ( cin >> word )
// ...
|
by: Steven (remove wax and invalid for reply) |
last post by:
I'm moving an old page to a new host, and trying to modernize it at the
same time. There are two little problems that are driving me nuts.
1. Validation of www.marzolian.com/index.html says:
Line 33, column 5: element "NOTE" undefined
<note>Last revised: May 29, 2005</note>
"You have used the element named above in your document, but the
document type you are using does not define an element of that name."
|
by: John N. |
last post by:
Hi All,
Here I have a linked list each containing a char and is double linked.
Then I have a pointer to an item in that list which is the current
insertion point.
In this funtion, the user hits the right
and left keys to move this insertion point (cursor)
Here is the problem:
|
by: placid |
last post by:
Hi all,
i have this struct for a binary tree node
typedef struct treenode
{
char *word;
struct treenode *right;
struct treenode *left;
}TreeNode;
|
by: Raj Singh |
last post by:
I am facing a problem with Visual C# 2003 and MS Word XP. I am using a
customized template for Word and in that template I have defined some
bookmarks. I am trying to create a Word document based on the customized
template, reading all the bookmarks and replacing them with desired
values, resulting in a new one page document being created which is then
saved. This all works fine but when I try to extend the process so that
the template...
| |
by: Fabrice |
last post by:
Hello,
(Alain)
Tis is a part of my code to retrieve text from hastable in memory cache, by
reading (befor) a resources file.
Thanks for your help.
/1/ The resources file
* I have create a .TXT file with Keys/Values
|
by: tvin |
last post by:
Hi all
I brought a string from a .txt file which was saved like utf-8.
In the .txt file i have this string "frédéric".My problem is that when i
read this file .txt,the bytes of é are like this : 101,180.
é length are 2 in utf-8 file.how can i change this 2 length to 1.
my problem that i want to use é like 233 byte and not like 2 bytes 101,180
|
by: Jerim79 |
last post by:
Here it is:
<?php
if($_SERVER=='POST'){
$Number=$_POST;
$Email=$_POST;
$Number2=0;
$error=0;
|
by: Jm.GlezdeRueda |
last post by:
Hi all,
Im trying to read a 24bit bmp with fread, and i have some problems.. I
want to read the whole structure in one time, but i dont know why, it
only reads the first member well..
I have two questions..
1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1,
sizeof(struct bmp), 1, fin); i have a Segment violation ??
|
by: arnuld |
last post by:
I have a function named getword that read every single input from std.
input.
WHAT I WANTED: I want it read the word if it has less than or equal to 30
characters. Anything else beyond that should be discarded and it should
ask the user for new input. Ctrl-D should exit from the program.
WHAT I GOT: It reads anything beyond 30 characters as the next word :\ and
I have to press Ctrl-D two times to exit from the program.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |