C++ :: Creating Vector Array And Reading A File
Apr 23, 2013Creating a Vector Array + Reading a file ....
View 1 RepliesCreating a Vector Array + Reading a file ....
View 1 RepliesIn a numerically intensive code, I have a Cartesian vector class Vector3d which has the normal operator overloading and basic functions such as dot product, magnitude, etc. For simplicity, assume that it is not a templated class and its components are of type double.
I frequently need large 1-d arrays (e.g. stl vectors) of Vector3d. Two use-cases must be satisfied:
1) The trivial case in which the data are stored as stl vectors of Vector3d;
2) The more difficult case where the individual components are stored as stl vectors of double, and are not guaranteed to be contiguous in memory (so one cannot rely on "stride").
Assuming the array lengths are all identical, I'd like to be able to access both types in a single loop. The straightforward way for case 2) is to construct a temporary Vector3d from the three components (indexed with the loop index). However, I would prefer not to incur the overhead of the constructor.
Is it possible using template metaprogramming. Ideally I'd like a CompositeVector3d that inherits from Vector3d and is constructed with the component vectors, but can be dereferenced using the loop index in the same way as one would do with case 1.
I am not looking for the typical template metaprogramming utility of being able to operate on the entire array without explicit loops. I just want the syntactic "sugar" whereby CompositeVector3d and Vector3d act the same, plus the avoidance of the cost of the constructor. I am not averse to using an additional templated class (perhaps a Field or a View class) to access the basic storage in both case.
Is it possible to do this without using a full template metaprogramming utility?
My program writes a vector to a file in binary. I want to erase the vector, then read the file to repopulate the vector then display.
Basically I want to erase the RAM memory and be able to use the file as memory. This is my current code but it is not reading the file correctly. I have checked the file to see that the writing part is working.
void read_from_file(vector<Info> &vector)
{fstream file;
file.open("info.dat", ios::binary | ios::in);
if (file.fail())
{
cout<<" FILE DOES NOT EXIST ";
system("pause");
[Code]...
Reposting this as I deleted my pervious post because I missed out some parts of the codes.
I am trying to read a textfile containing some informations in this format,I am able to extract everything except the itemId.
if I want load the file as vector and I want to show on the screen using the new loop for why it only show the last lopp this is a example of my thought why is this not right?
int numb[3];
ifstream output;
output.open("1.dat");
while(!output.eof())
{
output>>numb[3];
}
//assuming there are 3 number in vector
for(int i=0; i<2;i++)
{
cout<<numb[i];
}
I know to read a strings into array and tables.. what is they are mixed up?? strings are just names ( 3 characters) and there are bunch of table.. the max size was set to 60
ex. text file
JES
DAN
JEN
.
.
.
01010101
10010101
RAM
JET
01010010
10100101
.... and so on
should i use a while loop?
I was trying to implement own vector class and wrote some code. Below is the code.
Code: #include<iostream>
#include<vector>
using namespace std;
template <typename T> class MyVector
{
T *mem;
int m_size,final_size;
public:
MyVector() : final_size(1),m_size(4)
{
[code].....
I have question on this function.
Code: myVecPush_back(T t){}
Here I was able to put elements upto any number of time, but I have allocated memory to only 4 elements in T *mem.
My question, why the program is not crashing when I tried to put elements more that 4?
Is since the variable is of type Template? Any specific reason for this?
i've been trying to write array into file and read them into the same program again, but the output is all wrong.
Code:
#include<stdio.h>
int main()
{
FILE *fp;
char name[3][7];
int x;
}
[code]....
Is it possible to create a temporary
std::list of pointers
I would like to pass a temporary
std::list
to the constructor of a class to initialize its own one.
For example, using a
std::vector
:
#include <iostream>
#include <vector>
void func(const std::vector<int*>& myVec) {
for(int i=0; i<myVec.size(); ++i){
[code]....
Can we do this? What are other possible problems in addition the ones I have just mentioned above?
This is what I am supposed to be doing with this problem: Read the integer values in the file "numbers.dat" and display
* them in reverse order (that is, display the last number in the
* file first, the second-to-last second and so on). Use an array to
* store the values. It is safe to assume that the file contains no
* more than 100 values.
I have gotten far enough to read the file and display as an array, but it is displaying vertically rather than horizontally. So it is displaying 10 and a 1 on the first line then the 0 on the second line. Before I can work the reverse part out, I need it to display each number as 10 line 1 -235 line 2 so on and so forth.
This is my code so far:
void display_array_reversed() {
ifstream fin ("numbers.dat");
if (!fin){
cerr << "Error opening file" << endl;
exit(1);
[Code] .....
So if I have a file that has a list of scores and the name of the student, how am I going to read the scores and names, and print to output? For example, the text file will look like:
83 75 89 90 75 67 John Doe
67 92 78 64 88 95 Jane Waters
76 65 87 70 97 76 Billy Bob
And they'll be printed like that too.
I made a 2D array for the scores and names, and was able to scan the scores into the scoreArray, but I am stuck for the names. Do I use fscanf, fgets, etc? And how do I use these for a char array?
insert Code:
int main {
int scoreArray[NUM_ROW][NUM_COLUMN];
char nameArray[NUM_ROW][SIZE];
...........opened file, etc.
[ for (row=0; row<2; row++)
{ if(row>0)
{printf("
[Code] ....
When I run this, I get the scores and names (somewhat) in a jumbled mess.....
My assignment is to read in from a .txt file two things: an integer and a string. After reading in these 2 items I have to put them into a 10x10 2D array so that both the number and the character can be manipulated by the user. Here are the contents of the text file:
Dr. J's Garden
0.21,B, 2.80,G, 4.96,B, 2.66,B, 4.48,B, 0.61,T, 0.40,B, 3.50,G, 3.63,B, 3.91,T,
2.33,T, 4.51,G, 1.15,G, 4.95,T, 2.76,T, 4.51,T, 2.54,G, 4.04,T, 2.38,B, 0.62,B,
4.54,G, 3.38,T, 1.57,T, 3.92,T, 3.03,B, 4.72,G, 0.23,B, 4.02,G, 4.69,G, 0.66,G,
1.34,T, 2.21,G, 2.48,G, 4.85,T, 3.25,G, 3.55,G, 4.78,B, 0.81,G, 0.74,G, 2.55,G,
3.35,G, 4.52,G, 4.81,G, 2.67,G, 4.97,B, 0.87,G, 1.28,G, 4.58,B, 1.91,B, 3.69,B,
3.02,T, 3.15,T, 1.08,T, 4.68,G, 1.10,B, 3.17,G, 1.97,T, 0.99,G, 4.50,T, 3.87,T,
3.36,G, 1.60,T, 3.73,G, 2.14,B, 3.68,B, 2.44,G, 3.10,G, 4.54,B, 0.25,B, 0.25,G,
1.79,B, 3.02,G, 2.21,T, 0.22,G, 3.67,B, 4.46,G, 2.14,G, 2.31,G, 0.80,T, 3.83,B,
1.56,B, 1.41,T, 0.80,G, 1.27,T, 0.08,B, 1.20,G, 2.88,B, 2.78,T, 3.30,B, 1.75,B,
2.60,G, 4.72,T, 4.55,T, 0.89,B, 0.52,B, 2.06,T, 0.28,G, 4.36,T, 4.41,G, 0.36,B,
One number and one character need to be assigned to each element of the array. I know exactly how to fill a 2D array when it's just numbers, but the characters and the commas are giving me a lot of trouble.
i'm reading a file which has 20 rows and random number of columns, i want to put them in array/vector which i did but the problem is that array is filling a cell with a garbage value at location where i don't have a value in the data file.
suppose i have data file like following (there is tab between each column and each row has different number of columns)
Code:
20 30 10
22 10 9 3 40
60 4
30 200 90
33 320 22
here is my code
Code:
#include <vector>
#include <iostream>
#include <fstream>
[Code]....
In my problem I am to create a base class to represent a four vector (a concept in physics involving a four dimensional vector) then create a derived class specifically to represent the four momentum of a particle which inherits from the base class. I have been supplied a small piece of code to use to generate a 'random' x y and z component of the momentum magnitude. The code is as follows
#include <cstdlib>
double triangular(double momentum){
double x, y;
do{
x = momentum*rand()/RAND_MAX;
y = x/momentum;
} while (1.0*rand()/RAND_MAX > y);
return x;
}
It is said in my problem that this code is supposed to generate the magnitude, and then randomly split into x, y and z components. This code returns a single value and so I cannot see how it is doing what it says in the problem.
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct inventory
[Code]...
This is my program output Enter the file name : "filename".txt ..... (Nothing shows up)
why when I print out "array[2]" nothing prints? It just prints blank space. My file definitely has text in it, but when I try to assign "text" into the array of pointers it won't show any text. I know fgets() appends a newline at the end of the string, not sure if that has anything to do with it, but I've tried printing everything that should be in "array" with a for loop and I get nothing.
Code:
#include <stdio.h>
#define FILEPATH "/home/user/Desktop/text"
int main(){
FILE *myFile = fopen(FILEPATH, "r");
if(myFile == NULL) return 0;
char text[100];
char *array[100];
int idx = 0;
while(fgets(text, 100, myFile)){
array[idx++] = text;
}
puts(array[2]);
}
I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:
78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc
There's suppose to one array of companies, where each company will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in an array of structures.
My question is, how do I read the data from the file and put the data from each line into the next structure in the array of structures? I read the numbers in fine. I just use:
Code: fscanf(fp, "%f", &companyAry[i].hourlyPrices[j]);
But my program crashes when I try to read the name.
Code: fscanf(fp, "%[^]", &companyAry[i].companyName);
I'm thinking it has something to do with the fact the companyName is a pointer.
My structure looks like this:
Code:
typedef struct {
char *companyName;
float hourlyPrices[NUM_PRICES];
float avgPrice;
}
COMPANY;
My problem is that I need to take a csv or excel file with tens of thousands of datapoints, write all points in a section of column to an array, perform calculations on them, and then write it back into another column.
I've been looking all over the internet for ways to do this easily. So far I have not found anything that I can follow and implement.Some codes have been slightly useful, but they aren't commented in a way I can understand. How to write code to take, for instance, lines from column b between 500-1000, write them to an array, and write another array 500 characters in column c. If that code could be commented, I have tried a few different techniques (counting commas), but haven't gotten anything to work.
I'm having issues reading to a 2d array from a file. When I try to read the data from my file into my matrix variable it simply doesn't read anything and leaves the variable unmodified. I've tried just reading the first piece of data in the main function and it doesn't work there either. I'm really perplexed at this point since I've never had an issue reading from a file before. Here's my relevant code:
Code:
int main(int argc, char** argv) {
double matrix[MINSIZE][MINSIZE]={0}, vectors[MAXSIZE][MINSIZE], ans[MAXSIZE][MINSIZE];
FILE * inFile;
inFile = fopen(FILENAME,"r");
if(inFile==NULL){
printf("File does not exist.
[Code] .....
Is something like this possible and if not how can I make this work?
fscanf(in, "%d %d %d", &i, &x, &arr[i+x]);
I am working in Eclipse, and it keeps giving me this error that I do not understand. In the fillTable function, "is >> kP[i]" Eclipse says: no match for 'operator>>' in 'is >> *(((TranslationTable<int, std::string>*)this)->TranslationTable<int, std::string>::kP + (+(((unsigned int)i) * 8u)))'.
#ifndef TRANSLATIONTABLE_H_
#define TRANSLATIONTABLE_H_
#include "KeyValuePair.h"
#include <iostream>
#include <cstdlib>
[Code]....
Reading in a maze into a 2D array. The first two reads will give me the dimension of the maze(ex. m x n maze). So in order to create the 2D array i need the first two reads. Then after it is created it will read the rest of the data which are 1s and 0s. I have to create a program that will solve the maze but i cant test my movement code if i cant read in the data first. the entire program compiles but to test if i read the file i have a function to print it. But it says "There are: 0 rows and 0 columns " so it didn't read anything since rCount and cCount are initialized to 0. and basically the maze has nothing in it.
Code:
#include <iostream>
#include <fstream>
#include <stack>
#include <cstdlib>
#include "position.h"
[Code] .....
I have a text file with scores and names.
I have to read the score into an array and the names into an arrays also
27,Tom Jefferson
23,Ozzie Osborne
18,Wilt Chamberlain
15,Marie Antoinette
I've gotten the score to display correctly, but i cant get the full names. My function only reads the first names
I started making something for my class and the thing im getting stuck with is this function:
void ucitajOdgovore(string asocijacija[21]){
int brojac=0;
ifstream fajl;
string putanja;
/*srand(time(NULL)); int random=(rand() %5) +1;
switch(random){
[Code] ....
In the main, i pass real string array "asocijacije" in function which i use in it, and when i use it after this fun. i get an empty array. Its like it didnt happend and i cant see where i went wrong.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct inventory {
int barcode;
char description[512];
[Code] ....
This is my text file
20123451
Sugar packet (1 kg)
2.50
560
21347652
Milo 3-in-1
12.50
200
20123453
Salt (1kg)
4.50
270
21347654
Nescafe 3-in-1
12.50
400
20123455
Tamatoes
4.00
100
21347656
Cucumbers
4.00
280
This is my program output
Enter the file name : "filename".txt
..... (Nothing shows up)
The first part of the exercise is to read the lines from a file placing each line into an array. I thought my code looked correct however nothing but garbage prints out. Here's my code.
#include <iostream>
#include <fstream>
#include <string>
[Code]...