My input .txt looks as follows:
6 3
0 1 5
0 2 1
0 5 53
From the second line onwards I want to store the columns in arrays, so I did the following:
main(int argc, char** argv)
{
std::ifstream infile("instance1.txt");
int NumOne, NumTwo;
if (infile.good()){
infile >> NumOne >> NumTwo;
}
int Array1[NumTwo];
int Array2[NumTwo];
int Array3[NumTwo];
for(int i = 1; i < NumTwo + 1; i++){
infile >> Array1[i-1] >> Array2[i-1] >> Array3[i-1];
}
infile.close();
cout<<"first number"<<NumOne<<endl;
cout<<"second number"<<NumTwo<<endl;
for (int i=0; i < sizeof(Array1); i++){
cout << Array1[i] << " " << Array2[i] << " " << Array3[i] << endl;
}
cout<<"first array"<<Array1<<endl;
cout<<"second array"<< Array2<<endl;
cout<<"third array"<< Array3<<endl;
}
My output is the following:
first number6
second number5
0 1606413088 0
0 1 5
0 2 1
0 5 53
6923 5 1606414340
1 0 32767
1606673872 0 1606413088
32767 0 1
1606594089 0 2
32767 0 5
0 1 4
65793 2 5
80256 6923 5
0 1 0
80256 1606673872 0
0 32767 0
17623816 1606594089 0
1 32767 0
first array10x7fff5fbfeb10
second array20x7fff5fbfeaf0
third array0x7fff5fbfead0
Does anyone know where these numbers come from? I'm new to C++ and appreciate any hints about what goes wrong here.
I am not sure how this code already compiled but:
Issue One:
cout<<"first array"<<Array1<<endl;
cout<<"second array"<< Array2<<endl;
cout<<"third array"<< Array3<<endl;
I think this will display the hexadecimal address of each array, since you didn't type which element Array[?] in every array you are tying to display.
Issue Two:
for(int i = 1; i < NumOne + 1; i++){
infile >> Array1[i-1] >> Array2[i-1] >> Array3[i-1];
This code will set junk data, since it's going to try reading NumOne + 1 means 6 + 1 = 7 lines as max while you only have 3 lines. So what are you trying to do here?
Issue Three:
you need to set a fixed size before declaring any array. So int Numone = ? or Array[?]
Try this part of using vectors if you need to set the size of your container during run time
#include <vectors>
#include <iostream>
int main(int argc, char** argv)
{
std::ifstream infile("instance1.txt");
int NumOne, NumTwo;
if (infile.good()){
infile >> NumOne >> NumTwo;
}
std::vector<int> MyVectorOne(NumTwo);
std::vector<int> MyVectorTwo(NumTwo);
std::vector<int> MyVectorThree(NumTwo);
for(int i = 1; i < NumTwo + 1; i++){ // again this is wrong. What do you want here??
infile >> MyVectorOne[i-1] >> MyVectorOne[i-1] >> MyVectorOne[i-1];
}
infile.close();
cout<<"first number"<<NumOne<<endl;
cout<<"second number"<<NumTwo<<endl;
for (int i=0; i < MyVectorOne.size(); i++){
cout << MyVectorOne[i] << " " << MyVectorTwo[i] << " " << MyVectorThree[i] << endl;
}
// and here? what are you trying to display? this is also wrong
cout<<"first array"<< MyVectorOne<<endl;
cout<<"second array"<< MyVectorTwo<<endl;
cout<<"third array"<< MyVectorThree<<endl;
return 0;
}
Related
I have this section of code for generating an array of randomly generated and sorted ints, but the nosearch cin quits the program. Calling Cin.ignore() & cin.clear() before and/or after this line does not correct this, no error message is shown, the program just quits as soon as I hit enter on that section. This is the only statement to do this.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include "search.hpp" //a function called sarch is here, program quits before sarch is called.
using namespace std;
int main () {
srand(time(0));
int tenn;
int minrange;
int maxrange;
cout << "Enter the number of integers: ";
cin >> tenn;
cout << endl << "Enter the lower limit: ";
cin >> minrange;
cout << endl << "Enter the upper limit: ";
cin >> maxrange;
int *arr = new int(tenn*10);
for (int i = 0; i < tenn*10; i++){
*(arr+i) = (rand() % maxrange) + minrange;
}
cout << "list, unsorted: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
for (int i = 0; i < tenn*10; i++){
int low = i;
for (int j = i; j < tenn*10; j++){
if (*(arr+j) < *(arr+low)){
low = j;
}
}
int temp = *(arr+i);
*(arr+i) = *(arr+low);
*(arr+low) = temp;
}
cout << "sorted list: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
int nosearch;
cout << "Enter the number of values you wish to search for: " << endl;
//The next cin will fail
cin >> nosearch;
cout << "v";
//more code here...
I use a makefile to compile the code, and when I run it in terminal, this is what happens:
>./Needle.exe
Enter the number of integers: 2
Enter the lower limit: 0
Enter the upper limit: 10
list, unsorted:
9 4 6 2 4 5 2 9 1 4 7 8 3 4 9 4 6 7 9 9
sorted list:
1 2 2 3 4 4 4 4 4 5 6 6 7 7 8 9 9 9 9 9
Enter the number of values you wish to search for:
3
>
I can only assume something I did above triggered this error, which is why I included so much of the code.
I am trying to write a C++ program to ask user to input a list of 5 numbers and print out the counts of the first number in the input. I have been trying to use array[] but I have some problems. The ideal inputs and outputs are :
Input : 1 1 2 3 1 Output: 3 because there are 3 counts of 1
Input : 1 2 3 4 5 Output: 1
Input : 1 1 1 0 0 Output: 3
Here are my codes, my code allows me to take the inputs but it does not do anything with it. Any help is appreciated!
#include <iostream>
using namespace std;
//frequency function
int frequency(int a[])
{
int count = 0;
for (int i=0; i < 6; i++)
if (a[i] == a[0])
{
count++;
}
cout << count << endl ;
return count;
}
// Driver program
int main() {
int i;
cout << "Please enter your numbers: ";
int a[5] = {a[0],a[1],a[2],a[3],a[4]};
for (i = 1; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[0] >> a[1] >> a[2]>> a[3] >> a[4];
return 0;
}
int n = sizeof(a)/sizeof(a[0]);
cout << frequency(a);
}
I tried another simpler approach but it needs a little more help
#include <iostream>
#include <string>
using namespace std ;
int main(){
cout << "Please enter your numbers: ";
int a[5];
int repeat;
int first = a[0];
int i;
for (i = 0; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
if (a[i] == first){
repeat++;
}
cout << "Count: " << repeat;
}
you have an odd mixture of techniques for reading a set of numbers. You simply need
cout << "Please enter your numbers: ";
int a[5];
for (int i = 0; i < 5; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
and you certainly dont want that return as it will end the program
you would be better off using std::vector then you would not need to hard code the array size.
Note at the moment you have 6 as the arrays size in 'frequency' but 5 in main
My task is to ask the user for an int, then output a "number triangle" like the one below (in this case, the int is equal to 5).
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
However, the code which I have written for this task outputs this:
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
For reference, here is my code:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Size: " << std::endl;
cin >> size;
for(int i = size; i >= 0; i--)
{
for(int j = 0; j <= i; j++)
{
if (j < i){
cout << j << " ";
}
else{
cout << j << " ";}
}
cout << endl;
}
return 0;
}
Can somebody tell me what to change in my program to make it output the correct triangle? Thanks in advance.
You should print the spaces at the beginning of the line instead of at the end.
for(int i = size; i >= 0; --i){
for(int j = 0; j < size-i; ++j){cout << " ";} // spaces at the beginning
for(int j = 0; j <= i; ++j){
cout << j << " ";
}
cout << endl;
}
so im creating a vector with unlimted inputs but i want it so that when the user wants to stop inputing data, they just enter a non numerical key and then just move on to the next vector which is just the first one but reversed and multipled by 8 and 100 added to it but when it should move on, it just adds a bunch of zeros as inputs
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
vector<int> (myVec1)(n);
vector<int> (myVec2)(n);
int i;
int sum;
for(i=0;i<n;++i){
cout<<"please enter an integer ";
cin>>myVec1.at(i);
cout<<endl;
if(cin.fail()){
for(i=0; i<n; i++){
cout<<myVec1.at(i)<<" ";
}
}
}
cout<<endl;
for(i=0; i<n; i++){
sum=(myVec1.back()*8)+100;
myVec1.pop_back();
cout<<sum<<" ";
}
return 0;
}
this is the result and as you see theres just a bunch of zeros after i tried to stop the inputs if any one can help me id really appreciate it :)
please enter an integer 435
please enter an integer 412
please enter an integer 43
please enter an integer 56
please enter an integer 2
please enter an integer q
435 412 43 56 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 116 548 444 3396 3580
int n has undefined value (not initialized)
Instead, ignore defining n and initialize your vector like this: vector<int> myVec1;
and when you read values, append them in the vector using myVec1.push_back(valueFromKeyboard);
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> myVec1;
vector<int> myVec2;
int valueFromKeyboard;
cout << "please enter an integer ";
while (cin >> valueFromKeyboard) {
if (!cin.fail()) {
myVec1.push_back(valueFromKeyboard);
}
cout << "please enter an integer ";
}
for (int i = 0; i < myVec1.size(); i++) {
cout << myVec1[i] << " ";
}
cout << endl << endl;
for (int i = 1; i < myVec1.size()+1; i++) {
myVec2.push_back(myVec1.end()[-i]*8+100);
}
for (int i = 0; i < myVec2.size(); i++) {
cout << myVec2[i] << " ";
}
return 0;
}
myVec1.end()[-1] is the last element of the vector, myVec1.end()[-2] is the second to last etc. That's why I created this loop for (int i = 1; i < myVec1.size()+1; i++) to go from 1 to myVec1.size()+1
I would like to pass a 2D array to another function. I have an example of my code that I have all in the main function. However, the assignment requires that we split the code into a total of three functions, the main, adjmatrix, and adjlist functions.
All in the main function
#include<iostream>
#include<fstream>
using namespace std;
int main(void)
{
ifstream in;
char infile[40];
int c, u, v;
cout << "Please enter the input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
while(in.fail()) {
cout << "Please enter a CORRECT input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
}
//adj matrix
cout << "Adjacency Matrix" << endl;
in >> c;
int array[c][c];
for(int i=0; i<c; i++) {
for(int j=0; j<c; j++) {
array[i][j] = 0;
}
}
while(in >> u >> v) {
array[u][v] = 1;
}
cout << c << endl;
for(int i=0;i<c;i++) {
cout << i << " ";
for(int j=0;j<c;j++){
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
//adj list
cout << "Adjacency List" << endl;
cout << c << endl;
for(int i=0;i<c;i++) {
cout << i << " --> ";
for(int j=0;j<c;j++) {
if(array[i][j] == 1) {
cout << j << " ";
}
}
cout << endl;
}
in.close();
return 0;
}
This program outputs an adjacency matrix and adjacency list from the following input file
9
2 8
0 6
8 5
2 4
3 1
2 3
4 1
6 1
2 6
7 5
1 7
The following is the output
Adjacency Matrix
9
0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1 0
2 0 0 0 1 1 0 1 0 1
3 0 1 0 0 0 0 0 0 0
4 0 1 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 1 0 0 0 0 0 0 0
7 0 0 0 0 0 1 0 0 0
8 0 0 0 0 0 1 0 0 0
Adjacency List
9
0 --> 6
1 --> 7
2 --> 3 4 6 8
3 --> 1
4 --> 1
5 -->
6 --> 1
7 --> 5
8 --> 5
I read somewhere that passing a 2D array requires the second dimension to be entered. I also read something about it having to be a global constant? So I may have gotten a little crazy and went off on the crazy path on trying some things so please excuse some of the stupidity. The problem I think I am having is the actual array size comes from the file so I dont really understand where to go for declaring an array's second dimension with an actual value when int c is initialized to the first value in the input file. The following is my failed attempt at passing a 2D array. Enjoy:
#include<iostream>
#include<fstream>
using namespace std;
const int c;
void adjmatrix(istream &in, int array[][c]);
int main(void)
{
ifstream in;
char infile[40];
cout << "Please enter the input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
while(in.fail()) {
cout << "Please enter a CORRECT input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
}
in >> c;
int array[c][c];
adjmatrix(in, array);
in.close();
return 0;
}
void adjmatrix(istream &in, int array[][c])
{
int u,v;
for(int i=0; i<c; i++) {
for(int j=0; j<c; j++) {
array[i][j] = 0;
}
}
while(in >> u >> v) {
array[u][v] = 1;
}
cout << c << endl;
for(int i=0; i<c; i++) {
cout << i << " ";
for(int j=0; j<c; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
You can allocate the array dynamically instead of statically defining it.
int i;
int **array;
in >> c;
array = new int*[c];
for(i=0;i<c;i++)
array[i] = new int[c];
adjmatrix(in, array,c);
The declaration of the function adjmatrix will be adjmatrix(istream &in, int **array,int c);
Following may help:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
void print_adjacency_matrix(const std::vector<std::vector<int>>& mat)
{
std::cout << "Adjacency Matrix" << std::endl;
std::cout << mat.size() << std::endl;
for(std::size_t i = 0; i != mat.size(); ++i) {
std::cout << i << " ";
for(auto e : mat[i]) {
std::cout << e << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void print_adjacency_list(const std::vector<std::vector<int>>& mat)
{
std::cout << "Adjacency List" << std::endl;
std::cout << mat.size() << std::endl;
for (std::size_t i = 0; i != mat.size(); ++i) {
std::cout << i << " --> ";
for(auto e : mat[i]) {
if (e == 1) {
std::cout << e << " ";
}
}
std::cout << std::endl;
}
}
int main()
{
std::ifstream in;
std::string infile;
std::cout << "Please enter the input data file name(NO SPACES): ";
std::cin >> infile;
in.open(infile);
while(in.fail()) {
std::cout << "Please enter a CORRECT input data file name(NO SPACES): ";
std::cin >> infile;
in.open(infile);
}
int c;
in >> c;
std::vector<std::vector<int> > array(c, std::vector<int>(c));
int u, v;
while(in >> u >> v) {
array[u][v] = 1;
}
in.close();
print_adjacency_matrix(array);
print_adjacency_list(array);
return 0;
}