i need help din adding the elements of the 2 column.
the out of the 2nd column should be 2(bcos 1+1). add the values of each cell(sum =2). how to add the elements of a column. the loop should move down the column and add the values
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
char text[6][6];
ifstream stream1("c:\\cpptestdata.txt");
if(!stream1)
{
cout<<"Cannot read file\n";
}
while(!stream1.eof())
{
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
stream1>>text[i][j];
}
}
}
//checking if it has been correctly inserted.
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
cout<<text[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"first two rows:"<<endl;
int i,j;
for (i=0; i<2; i++){
for (j=0; j<6; j++){
std::cout<<text[i][j]<<"\t"<<' ';
}cout<<endl;
}
cout<<"find immediate neighbours of A:"<<endl;
char largest=text[1][1];
for(i=0; i<6; i++){
for(j=1; j<2; j++){
if(text[i][j]>largest)
cout<<text[i][0]<<"N"<<"\t";
else
cout<<"0";
}cout<<endl;
}
cout <<" finding k neighbours for A : "<<endl;
for (i=1; i<6; i++){
int max = text[1][1]-'0';
for(j = 1; j<2; j++){
if(max < (text[i][j]-'0')){
max = text[i][j]-'0';
cout<<max;
}
else
cout <<"xx";
}cout<<endl;
}
return 0;
}
In general, if you want to access column c you write something like this:
for (int i = 0; i < 6; ++i) {
// access column c with text[i][c]
}
The following code is written to add the elements of the second column of your array, assuming the following things from your code:
It is a 6x6 array.
Instead of integer array, you are using a character array. (I didn't understand the need for this).
cout << "sum of elements\n";
int sum = 0;
for(i=0;i<6;i++)
{
sum = sum + (text[i][2] - '0'); //Convert char to int and add
cout <<" SUM = "<<sum<<"\n";
}
If no special reasons, define the array as an int array itself. Then you could directly add the values.
for(i=0;i<6;i++)
{
sum = sum + text[i][2];
cout <<" SUM = "<<sum<<"\n";
}
Related
I'm working on this code and I don't understand why reading a matrix from the file doesen't output the correct matrix but instead it just shoves all the values != 0 into a single array and doesen't print 0. Thanks.
#include <iostream>
#include <fstream>
#include <array>
using namespace std;
#define M 4
#define N 4
int** inputArray();
void printArray(int** matrix);
int main()
{
int **matrix;
matrix = inputArray();
printArray(matrix);
return 0;
}
void printArray(int** matrix)
{
for (int i=0; i<M; i++)
{
for(int j=0; j<N; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
}
int** inputArray()
{
int** matrix=new int*[N];
int value;
ifstream matrice("matrix.txt");
if (matrice.is_open())
{
do
{
for (int i=0; i<N; ++i)
{
matrix[i]=new int[M];
for(int j=0; j<M; j++)
{
matrice >> matrix[i][j];
//cout << matrix[i][j];
}
}
}
while (matrice>>value);
matrice.close();
return matrix;
}
else cout << "Unable to open file";
}
The matrix I tried using is:
3418
0163
0023
0001
The program should print out exactly the matrix I'm reading from the file.
Your input processing way is wrong ! As each element in a row is not separated by space you should take input as string or character.
For example:
#include <iostream>
#include <fstream>
#include <array>
using namespace std;
#define M 4
#define N 4
int** inputArray();
void printArray(int** matrix);
int main()
{
int **matrix;
matrix = inputArray();
printArray(matrix);
return 0;
}
void printArray(int** matrix)
{
for (int i=0; i<M; i++)
{
for(int j=0; j<N; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
}
int** inputArray()
{
int** matrix=new int*[N];
int value;
ifstream matrice("matrix.txt");
if (matrice.is_open())
{
for (int i=0; i<N; ++i)
{
string x;
matrice>>x;
matrix[i]=new int[M];
for(int j=0; j<M; j++)
{
matrix[i][j]=x[j]-'0';
//cout << matrix[i][j];
}
}
matrice.close();
return matrix;
}
else cout << "Unable to open file";
}
I used string here. matrice>>x will take input until it get white space or newline.So matrice>>x will input a whole row as string in x. After taking input I iterated through the string and converted each character to number using x[j]-'0' and added each element on its cell.
I'm trying to figure out how to pass a 2 dimensional Matrix to a function
//function to print Matrix
void refl(int n, int board[][])
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
int board[n][n]; //creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
refl(n , board);
}
return 0;
}
It says that "n" and "board" isn't declared in the function while they are .
Try to use C++ std::vector or good old C malloc+free.
C++
#include <string>
#include <iostream>
#include <vector>
using namespace std;
void reflxx(int n, vector<vector<int>>& board)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
vector<vector<int>> board(n, vector<int>(n));
//creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
}
reflxx(n , board);
return 0;
}
See http://www.cplusplus.com/reference/vector/vector/resize/ for more example.
I have it so that the user defines the size of the vector and then a vector is filled. Then that vector is sorted using bubble sort (homework assignment). However, when the "sorted" vector is displayed, there are different numbers in it than what were in the original creation. How do I first create the vector, display it, then sort it and display it??
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
size = data.size();
//Sorting
void bubbleSort(vector<int> & data);
{
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
First off:
make sure to include all necessary header files, e.g. stdlib.h for your used rand() function.
get rid of all unused variables, like average, median and size.
declare your bubbleSort function outside of main function, and add additional checkup code to prevent sort if list has not more than one element.
The sort problem is related to this code snippet of yours:
for (int i = 0; i<size -1 - k; i++) { ... }
Simply remove -1
To fix your sort problem, and for better output, use following code:
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
using namespace std;
void bubbleSort(vector<int>& data)
{
if(data.size() <= 1)
return;
for(int i=1; i<data.size(); i++)
{
for(int j=0; j<data.size()-i; j++)
{
if(data.at(j) > data.at(j+1))
{
int temp = data.at(j+1);
data.at(j+1) = data.at(j);
data.at(j) = temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout << "Vector Length?: ";
cin >> n;
// Filling vector
for(int i=0; i<n; i++)
data.push_back(rand()%10+1);
cout << "Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
// Sorting
bubbleSort(data);
cout << endl << "Sorted Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
return 0;
}
Hope this helps ;)
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
void printVector(vector<int> & data)
{
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
//Sorting
void bubbleSort(vector<int> & data);
{
size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
}
}
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
printVector(data);
bubbleSort(data);
printVector(data);
}
If you are planning to define function void bubbleSort(vector & data) later you need to declare it before calling it.\
void bubbleSort(vector<int> & data);
int main()
{
// Here your code
bubbleSort(data);
//Here your code
}
You need to define variables only just before you need it. And if you declare and never use it, you will get unused variable warnings. So better you can comment all these variables. You can un-comment whenever you need.
//double average=0.0;
//int median = 0;
You should call your function bubbleSort() from main() as follows:
bubbleSort(data);
You are not using the iterator indexes properly to sort the elements of vector. If you change your function bubbleSort as follows it will work
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
complete program for your reference:
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
int main()
{
int n;
//double average=0.0;
//int median = 0;
//double size = 0.0;
//int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
// int n =10;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
bubbleSort(data);
std::cout<<"sorted vector"<<"\n";
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
I have a simple sorting program being compiled by Dev-C++ 4.9.8.0. I ran the program (yes this compiles) and it simply stops after displaying the line where the vector is displayed for the first time. Note - it does not freeze, it seems to just be taking a pause. In the code, the selection sort comes next so I assume that the error happens there, but there is no error message for me to even figure out what to do!
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
void bubbleSort (vector<int>& data)
{
if(data.size() <= 1)
return;
int flag=1;
int temp;
for(int i=1; (i<=data.size()) && flag; i++)
{
flag=0;
for(int j=0; (j<data.size()-1); j++)
{
if(data[j+1] > data[j])
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
flag=1;
}
}
}
}
void selectionSort(vector<int>& data)
{
int min, temp, n=data.size();
for (int i=0; i<n; i++)
{
min = i;
for (int j=i+1; j<n; j++)
{
if (j<min)
{
temp=i;
i=min;
min=temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout<<"Vector length?: "<<endl;
cin>>n;
srand(time(0));
for (int i=0; i<n; i++)
{
data.push_back(rand()%20+1);
}
cout<<"Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
selectionSort(data);
cout<<"Sorted Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
system("Pause");
return 0;
}
selectionSort() method has variable 'n' that is completely a random value that happens to be on the stack at that location. You haven't initialized it!
You have a nested loop, which is O(n^2). Say n is 1982734 or some such arbitrarily large number. You are simply looping over 1982734 * 1982734 times. EVENTUALLY it will complete. Why don't you print the value of 'n' inside selectionSort(). Just initialize it with the size of the vector.
As others commented this whole work is in progress.
I found some answers related to this problem already, but I don't really get how they work. I need to find the biggest sum of each row of a two dimensional and print it out, but don't print any of the other sums out. Here is what I have so far:
#include <iostream>
using namespace std;
int main (int argc, char ** argv)
{
int v[3][5]; // 3 rows, then 5 columns
for (int i=0; i<3; i++){
for (int j=0; j<5; j++){
cin >> v[i][j];
}
}
//test reading value
for (int i=0; i<3; i++){
for (int j=0; j<5; j++){
cout << v[i][j];
if (i!=4)
cout << ' ';
}
cout << '\n';
}
//solve it
for (int i=0; i<3; i++){
int sum = 0;
for (int j=0; j<5; j++){
sum += v[i][j];
}
cout << sum << '\n';
}
return 0;
}
This code right now prints out the sum of each row in the 2D array, but I need my program to print out just the biggest sum of the rows. This is the only problem I have right now. Thanks in advance.
You can accomplish this by keeping track of the largest sum computed for each row of the array.
//solve it
int largestSum = 0;
for (int i = 0; i<3; i++)
{
int sum = 0;
for (int j = 0; j<5; j++)
{
sum += v[i][j];
}
// check to see if we have computed a new larger sum and save it if we have.
if (sum > largestSum)
{
largestSum = sum;
}
}
cout << "largest sum: " << largestSum << '\n';
Alternatively if you are allowed to use other components in the C++ Standard library you can use std::accumulate to calculate the sum instead of using an inner look, and std::max to determine the largest sum between current and previously calculated sum.
#include <algorithm>
#include <numeric>
//solve it
int largestSum = 0;
for (int i = 0; i<3; i++)
{
int sum = std::accumulate(std::begin(v[i]), std::end(v[i]), 0);
largestSum = std::max(sum, largestSum);
}
cout << "largest sum: " << largestSum << '\n';
Here is the shortest solution I could come up with, and also correctly handling negative sums (C++11):
int maxSum = std::numeric_limits<int>::min();
for (const auto& row : v)
maxSum = std::max(maxSum, std::accumulate(std::begin(row), std::end(row), 0));
std::cout << maxSum << std::endl;
int biggestSum = 0;
for (int i=0; i<3; i++) {
int currentSum = 0; // sum of values in this row
for (int j=0; j<5; j++){
currentSum += v[i][j];
}
if ( currentSum > biggestSum) biggestSum = currentSum; // we have new max
}
cout << biggestSum << '\n';