C++ programming define array size from the external text file - c++

I am trying to make my array have a size of a non-constant value. The size should be defined by the "test.txt" file that gets the information from. For example, if the txt file has 10 numbers then the array should be in size of 10. I tried using vectors but I couldn't make it work. Any help would be much appreciated. Here is the code below:
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<cstdlib>
using namespace std;
/* Function to print an array
A int[]: an array of n element
n int; length of an array
*/
void displayList(float A[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << A[i] << ", ";
cout << endl;
}
/*
Insertion Sort function
A int[]: an array of n element
n int; length of an array
*/
void insertionSort(float A[], int n)
{
int i, j;
float key;
for (i = 1; i < n; i++)
{
key = A[i];// take key
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && A[j] > key)
{
A[j + 1] = A[j]; // move element to next postion
j = j - 1; // reduce index of j - go backward in the array
}
std::cout << "Step key at i = " << i << ": [" << key << "] inserted at j = " << j + 1 << "
position -> ";
A[j + 1] = key; // at j+1 th position we place the key
displayList(A, n);
}
};
ifstream input("test.txt"); //put your program together with thsi file in the same folder.
int main() {
int const ARRAY_SIZE = 9;
float A[ARRAY_SIZE];
string line;
ifstream inFile;
int i = 0, cnt = 0;
float n;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (!inFile.eof()) {
getline(inFile, line);
n = atof(line.c_str());
cnt++;
}
int cnt;
cin >> cnt;
vector<float> A(cnt);
inFile.close();
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (!inFile.eof()) {
getline(inFile, line);
n = atof(line.c_str());
A[cnt++] = n;
}
inFile.close();
n = sizeof(A) / sizeof(A[0]);
cout << "insertionSort: \n";
cout << "Unsorted array: ";
displayList(A, n);
insertionSort(A, n);
std::cout << "Sorted array: ";
displayList(A, n);
}
sample input from txt file:
12
4
5
9
6
11
0
2
0.5

To make it work with vectors you shouldn't create the vector with a number of elements, like vector<float> v(10);. Create an empty vector and add one value at a time to it.
void display(const std::vector<float>& A) {
std::cout << "Got " << A.size() << " elements.\n";
for(float value : A) {
std::cout << value << '\n';
}
}
int main() {
std::vector<float> A; // an empty vector of floats
float temp; // a temporary float to use for extraction
while(input >> temp) { // loop while extraction succeeds
A.push_back(temp); // save the value at the end of the vector
}
display(A);
}

Related

C++ Vectors Segmentation Fault

I am working on a program that has an input file and it either adds, removes, or prints the string. I have my print function working, but I am getting a segmentation fault when I uncomment the lines of code shown.
int main()
{
vector <string> vec; //Creates an empty vector
string command;
string word;
int index;
ifstream fin;
fin.open("datalabvec.dat"); //Opens the input file
if (!fin)
cout << "The input file does not exist" << endl << endl;
else
{
fin >> command;
while (fin)
{
if (command =="Add")
{
fin >> word >> index;
//addVec (vec, word, index);
}
//if (command == "Remove")
//{
//fin >> index;
//remVec (vec, index);
//}
// else //Print function
{
printVec(vec);
}
fin >> command;
}
}
}
void addVec(vector <string> &v, string word, int ind)
{
int size = v.size();
if (ind > size + 1)
cout << "Invalid adding at index " << ind << endl;
else
{
v.insert(v.begin()+ind, word);
}
}
void remVec(vector <string> &v, int ind)
{
int size = v.size();
if (ind > size)
cout << "Invalid removing at index " << ind << endl;
else
{
v.erase(v.begin() + ind);
}
}
void printVec(const vector <string> v)
{
int size = v.size();
for (int i = 0; i < size; i++)
{
cout << v[i] << "\t";
}
cout << endl;
}
Input File
Remove 0
Add Student 0
Add Kid 0
Add Final 1
Add Grow 1
Add Note 2
Add Bad 6
Remove 5
Add Worse -1
Print
Add Rich 5
Remove 1
Remove 7
Add Mind 2
Remove 3
Print
In your adding function. I guess mistakenly you wrote size+1 inside if statement. It should be size-1 because the last member of vector is at size-1. [0] - [size-1]. Correct statement is if(ind >= size) or ind > size-1. Like you used in for loop in writer function.
And as Brad suggested in comment below. It is good idea to check weather ind >= 0 or not.
void addVec (vector <string> &v, string word, int ind)
{
int size = v.size();
if (ind < 0 || ind >= size)
cout << "Invalid adding at index " << ind << endl;
else
{
v.insert(v.begin()+ind, word);
}
}
void remVec (vector <string> &v, int ind)
{
int size = v.size();
if (ind < 0 || ind >= size)
cout << "Invalid removing at index " << ind << endl;
else
{
v.erase(v.begin() + ind);
}
}

read int per line c++ error needs solution

hey guys I need help for my read code seems not working properly here's the code. The problem is as shown in the picture, the compiler are supposed to display all 1 million int value but it seems that my write or the display code was wrong. It shows nothing like it's not even reading.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <omp.h>
#include <ctime>
#include <cstdlib>
using namespace std;
int* CreateArray( int*);
void shellSortParallel( int*, int);
void shellSortSequential(int*, int);
void InsertSort( int*, int, int, int);
int main()
{
int array_size = 1000000;
int n=0;
int* arr=new int[array_size];
ifstream fin("OUTPUT1.txt");
if(! fin)
{
cout << "File could not be opened." << endl;
}
else
{
cout << "File Opened successfully!!!. Reading data from file into array" << endl;
int data;
while(fin>>data)
{
arr[n] = data;
n++;
}
cout << "Displaying Array..." << endl << endl;
for(int i = 0; i < array_size; i++)
{
cout << arr[i];
}
}
fin.close();
int length = 1000000;
double endTime = 0, startTime = 0, totalTime = 0;
double start, end;
cout << "Program is now sorting using shell sort" <<endl;
startTime = time(NULL);
start = omp_get_wtime();// Start performance timer 1 run
shellSortParallel(arr, length);//Run the algorithm
end = omp_get_wtime();// End performance timer 1 run
endTime = time(NULL);
totalTime = endTime - startTime;
cout << "This is the time it took to run. " << totalTime << endl;// time in seconds
int stupid = 0;
cin >> stupid;
cout << "Program has completed all tasks!!" << endl;
return 0;
}
void shellSortParallel(int array[], int length)
{
int h;
int j = 0;
int temp = 0;
int i = 0;
for(h =length/2; h > 0; h = h/2)
{
#pragma omp parallel for shared( array, length, h, i) default(none)
for( i = 0; i < h; i++)
{
//int ID = omp_get_thread_num();
InsertSort(array, i, length, h);
}
}
}
void InsertSort(int arr[], int i, int length, int half)
{
//cout << ID << " ";
int temp = 0;
int j = 0;
for (int f = half + i; f < length; f = f + half)
{
j = f;
while(j > i && arr[j-half] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-half];
arr[j-half] = temp;
j = j -half;
}
}
}
and here is the short version of the file that I'm going to read. Its a random number between 1 to 1million per line
2377763
88764877846
281327
60
625
86
646127818
14551
2177645
32033
1826761
555173
3415445377
32430
1101
any help would be much appreciate, thank you before
By if(fin>>data) you are not just testing, but retrieving data from stream. I suggest use ifs.good() for testing. Overall, you can write such a code instead
std::ifstream fin ("OUTPUT1.txt", std::ifstream::in);
char c = fin.get();
while (fin.good())
{
std::cout << c;
c = fin.get();
}
arr[n-1] = '\0'; is not correct because it's not an array of character so don't mind.
to correct it:
int data;
while(fin>>data)
{
arr[n] = data;
n++;
}
cout << "Displaying Array..." << endl << endl;
for(int i = 0; i < array_size; i++)
{
cout << arr[i];
}
why allocating such huge array of integers? use vector is a good thing:
vector<int> vec;
ifstream fin("OUTPUT1.txt");
int data;
while(fin >> data)
{
vec.push_back(data);
}
cout << "Displaying Array..." << endl << endl;
for(int i = 0; i < vec.size(); i++)
cout << vec[i] << endl;
fin.close();
88764877846 out band of an integer which causes the loop stop reading so you have to either get values as strings then convert into __int64 or __int128
to read values as strings:
string sLine;
int nLines = 0;
ifstream fin("OUTPUT1.txt");
// first read to get number of lines
while(getline(fin, sLine))
nLines++;
//create an array of strings
string* pstrValues = new string[nLines];
// set the get pointer to the beginning of file because the previous read moved it
fin.clear();
fin.seekg(0, ios::beg);
int i = 0;
while(getline(fin, sLine))
{
pstrValues[i] = sLine;
i++;
}
cout << "Displaying Array..." << endl << endl;
for( i = 0; i < nLines; i++)
cout << pstrValues[i] << endl;
fin.close();
now you have an array of strings convert it to int values but you must convert to __int64 because as I said there are values bigger than size of int (4bytes)

Moving elements in an array by certain amount

So I have a program that reads in a certain number of keys. An example would be
5 1 10 21 9 6 21 11 13 16 20
The text file example would be
Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2
I want my program to start at Java and depending on first key, which in this case is 5, would move over 5 elements leading to "red". And so on and so on. However, I have managed to read in all the data and put them into arrays, I am just having trouble figuring out how to move the pointer in the array.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct pieces {
char word[5];
int jump;
} ;
// Main Function
int main ()
{
// declare variables
int wordCount[2];
int keyCount[2];
int numKeys, numKeys2;
int numWords;
int keyAmount = 1;
int wordAmount = 23;
int keyarr[11];
pieces cypher[wordAmount];
char filename[10];
ifstream inData;
int temp[10];
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
inData.open(filename);
if(inData.is_open());
{
// read in data
for ( numKeys = numWords = 0; numKeys < keyAmount; numKeys++){
// read in num of words and keys
inData >> wordCount[numKeys] >> keyCount[numKeys];
//read in words followed by jump key
for( numWords = 0; numWords < wordAmount; numWords++){
inData >> cypher[numWords].word >> cypher[numWords].jump;
}
// read in the actual keys
for( numKeys2 = 0; numKeys2 < wordCount[numKeys]; numKeys2++){
inData >> keyarr[numKeys2];
}
}
//print out data
for( int j = 0; j < numKeys; j++){
cout << wordCount[j] << "\n";
cout << keyCount[j] << "\n";
}
cout << "\n";
for ( int i = 0; i < wordAmount; ++i){
cout << cypher[i].word << " ";
cout << cypher[i].jump << " ";
}
cout << "\nKeys: " << "\n";
for(int k = 0; k < 11; k++){
cout << keyarr[k] << " ";
}
cout << "\n";
}
inData.close();
return 0;
}

C++ Binary Search Not Working Correctly - Finds Element Not in Array

I am running the binary search algorithm in C++ but it gives me spurious results. For example, searching for the value 21 gives me a
"Value is Found"
message but my array consists only of numbers from 0 to 20.
Any help is greatly appreciated.
#include <iostream>
#include <iomanip>
using namespace std;
int binarySearch(const int [], int, int, int, int ); // function prototype
int main()
{
const int arraySize = 10;
int arr[ arraySize ];
int key;
for( int i = 0; i <= arraySize; i++) // generate data for array
arr[i] = 2*i;
cout << "The array being searched is: " << endl;
for (int j = 0; j<=arraySize; j++) // print subscript index of the array
{
cout << setw(5) << j << " ";
}
cout << endl;
for (int z = 0; z<=arraySize; z++) // print elements of the array below index
{
cout << setw(5) << arr[z] << " ";
}
cout << "\n" <<"Enter value you want to search in array " << endl;
cin >> key;
int result = binarySearch(arr, key, 0, arraySize, arraySize); // function call
if (result == 1) // print result of search
cout << "Key is found " << endl;
else
cout << "Key not found " << endl;
return 0;
} // end main function
int binarySearch(const int a[], int searchKey, int low, int high, int length)
{
int middle;
while (low <= high){
middle = (low + high) / 2;
if (searchKey == a[middle]) // search value found in the array, we have a match
{
return 1;
break;
}
else
{
if( searchKey < a[middle] ) // if search value less than middle element
high = middle - 1; // set a new high element
else
low = middle + 1; // otherwise search high end of the array
}
}
return -1;
}
You are invoking undefined behavior because your for loop conditions are <=arraySize. Change it to <arraySize. On making this change, the code works perfectly for sample inputs.
By writing int arr[ arraySize ]; you are creating an array of 10 elements (i.e., from 0 to 9), while in the for loops, you start from 0 and move until 10.
Live Demo

c++ arrays and functions and counters

My code is done and working. But i cant figure out how to count the number of attempts made by the user and invalid account numbers that were entered. I am supposed to do this in main starting after cin >> accountNum. After the user enters 9999 to quit, it is supposed to display the number of attempts made and the number of invalid charge account numbers that were entered. When i run it i get 0 for number of attempts and -1 for invalid numbers entered.
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
void getAccountNumbers(int[], int);
void displayAccountNumbers(int[], int);
void selectionSort(int[], int);
int binarySearch(const int[], int, int);
int main()
{
int accountNum;
int results;
int attempts = 0;
int invalidNumbers = 0;
const int ARRAY_SIZE = 18; // Array size
int numbers[ARRAY_SIZE]; // Array with 18 elements
int count = 0;
//ifstream inputFile;
getAccountNumbers(numbers, ARRAY_SIZE);
cout << "Original Order" << endl;
displayAccountNumbers(numbers, ARRAY_SIZE);
selectionSort(numbers, ARRAY_SIZE);
cout << "Sorted List" << endl;
displayAccountNumbers(numbers, ARRAY_SIZE);
cout << "********************" << endl;
cout << "Enter an Account number or 9999 to quit" << endl;
cin >> accountNum;
if(accountNum == 9999)
{
cout << "Thank You!" << endl;
}
while(accountNum != 9999)
{
results = binarySearch(numbers, ARRAY_SIZE, accountNum);
if(results == -1)
{
cout << "That number was not found" << endl;
invalidNumbers = results++;
}
else
{
cout << "That number is valid " << endl;
}
attempts = results++;
cin >> accountNum;
}
cout << "Number of attempts: " << attempts << endl;
cout << "Invalid numbers entered: " << invalidNumbers << endl;
system("pause");
return 0;
}
void getAccountNumbers(int nums[], int size)
{
ifstream inputFile;
int count = 0;
//Open the file
inputFile.open("charges.txt");
while(count < size && inputFile >> nums[count])
count ++;
//Close the file
inputFile.close();
}
void displayAccountNumbers(int nums[], int size)
{
for(int count = 0; count < size; count++)
cout << nums[count] << "\t";
cout << endl << endl;
}
void selectionSort(int nums[], int size)
{
int startScan, minIndex, minValue;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = nums[startScan];
for(int index = startScan + 1; index < size; index++)
{
if(nums[index] < minValue)
{
minValue = nums[index];
minIndex = index;
}
}
nums[minIndex] = nums[startScan];
nums[startScan] = minValue;
}
}
int binarySearch(const int nums[], int size, int value)
{
int first = 0, //First element
last = size - 1, // Last element
middle, // Midpoint
position = -1; //Position of search value
bool found = false;
while(!found && first <= last)
{
middle = (first + last) / 2; //Midpoint
if(nums[middle] == value)
{
found = true;
position = middle;
}
else if(nums[middle] > value) // Value is in lower half
last = middle - 1;
else
first = middle + 1; // Value is in upper half
}
return position;
}
Your problem is in the lines where you are trying to add to invalidNumbers and attempts. The ++ postfix operator adds one to the number before it. You needn't say invalidNumbers = results++;; you merely need invalidNumbers++;, and the same applies for attempts. What your code was doing was setting invalidNumbers (and attempts) to the value of results and then adding one to results instead.