How to add user input into a 2D array - c++

I'm trying to get get user input, which is stored in an array (eightBit[]), and then add that to a 2D array (board). The user is supposed to enter 8 numbers, for an example:
Byte 1: 1
Byte 2: 2
etc...
and the output is supposed to look like:
1 2 3 4
5 6 7 8
however this is the output I get:
8 8 8 8
8 8 8 8
Any idea why its repeating only the last numbered entered? Part of my code is below, any help would be appreciated.
cout << "Enter a pattern of eight bits:" << endl;
for(i = 0; i < 8; i++){
cout << "Byte " << i+1 << ": ";
cin >> eightBit[i];
}
int board[2][4];
for(i = 0; i<8; i++){
for(int j=0; j<2; j++){
for(int k=0; k<4; k++) {
board[j][k] = eightBit[i];
}
}
for(int j=0; j<2; j++)
{
for(int k=0; k<4; k++)
{
cout << board[j][k] << " ";
}
cout << endl;
}

That's because of your outer loop with i which is basically overwriting every element in your 2D array.
A solution would be to drop that outer loop entirely, like so:
int i = 0;
for(int j=0; j<2; j++) {
for(int k=0; k<4; k++) {
board[j][k] = eightBit[i++];
}
}
also you have bracket mismatch in your code snippet.

That's natural. In the second for when the i gets at last 8, then the board gets filled with the current i (i=8).
Try this, and next time be more careful with your code :).
#include <iostream>
using namespace std;
int eightBit[2][4];
int main()
{
cout << "Enter a pattern of eight bits:" << endl;
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
cout << "Byte " << (j+1)+4*i << ": "; //4 = # of columns,i=row,j=column.
cin >> eightBit[i][j];
}
}
int board[2][4];
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
board[i][j] = eightBit[i][j];
}
}
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
}

Related

Implementing Gauss Seidel iterative method on C++

I am trying to implement the Gauss seidel Iterative method in C++. i have a very messy code because i am still learning. for some reason my while loop seems to run without implementing the loop at all. I cannot get do while to work either.
The test matrix im using is
number of equations=2
x[0][0]=4
x[0][1]=2
x[1][0]=1
x[1][1]=3
b[0][0]=1
b[1][0]=-1
accuracy=0.2
the loop should continue until K has become less than accuracy.
The results should be;
x1=0.4583
x2= 0.4681
(i printed the F matrix just to make sure that part was working correct)
k=0.136
iterations performed=2
Again sorry for the messy code, like i said i am still learning.
Also i've tested the maths on separate codes and it works perfectly.
int main()
{
int n,i,j,p=0,l=0;
cout<<"Enter number of Equations = ";
cin>>n;
double a[n][n],b[n-1][1],F[n-1][1],x[n-1][1],T[n-1][1],e,k,B,C;
cout<<"[a].[x]=[b]"<<endl;
cout<<"Enter Matrix a:"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout<<"a["<<i<<","<<j<<"] = ";
cin>>a[i][j];
}
cout<<"Enter Matrix b:"<<endl;
for(j=0;j<n;j++)
{
cout<<"b[0,"<<j<<"] = ";
cin>>b[0][j];
}
cout<<"Enter the Accuracy = ";
cin>>e;
for (i=0;i<n;i++){
T[i][0]=0;
}
for(i=0;i<n;i++){
x[i][0]=0;
}
for(i=0;i<n;i++){
F[i][0]=0;
}
while (k>=e){
C=0;
p=p+1;
k=0;
for(i=0;i<n;i++){
B=0;
T[i][0]=(b[i][0]/a[i][i]);
C=a[i][i];
for (j = 0; j < n; j++) {
if (j!=i)
B=B+(a[i][j])*(x[j][0]);
}
x[i][0]=T[i][0]-(B/C);
}
cout<<x[0][0]<<endl;
cout<<x[1][0]<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++){
F[i][0]=F[i][0]+(a[i][j]*x[j][0]);
}
}
for(i=0;i<n;i++){
F[i][0]=(F[i][0]-b[i][0]);
}
for(i=0;i<n;i++){
k=k+((F[i][0])*F[i][0]);
}
k=sqrt(k);
}
for (i=0;i<n;i++){
cout<<"x"<<i+1<<"="<<x[i][0]<<endl;}
cout<<"number of iterations used: "<<p<<endl;
for (i=0;i<n;i++){
cout<<"F"<<i+1<<"="<<F[i][0]<<endl;}
cout<<"k="<<k<<endl;
return 0;
}
Edit: i tried giving k an initial value and that goes infinitely and the pro gram crashes.
Heres are a run down.
The 3 important Matrixes are a,x and b.
ax=b
each row is changed into an equation and solved for x(i) with initial value for all the position in matrix x to be 0.
after completing this for all values in matrix x the accuracy "k" is checked by function: ax-b=matrix F, All values of F are then squared, added and under root to get k which is checked against e.
the results are:
x1=0
x2=0
number of iterations used: 0
F1=0
F2=0
k=2.96439e-323
First please note that the method you implement is the Jacobi iterative method, not Gauss-Seidel method.
There were several issues in your code:
k was not initialized: it must me set to a large value before the while loop, and set to 0 prior the calculation inside this loop
Some vectors were not initialized at the good size: n-1 instead of n
The F[.] vector should be reset to 0 at each iteration, not only at the start of the program
Moreover, I made some other modifications
I replaced variable length arrays (not C++) by std::vector
I replace [n][1] arrays by [n] vectors
I tried to avoid some i, j global variables
Here is a working code:
#include <iostream>
#include <cmath>
#include <vector>
using std::cin, std::cout;
int main() {
int n, p = 0;
cout << "Enter number of equations = ";
cin >> n;
double e , k, C;
std::vector<double> b(n), F(n, 0), x(n, 0), T(n, 0);
std::vector<std::vector<double>> a(n, std::vector<double> (n));
cout << "[a].[x]=[b]" << "\n";
cout << "Enter Matrix a:" << "\n";
for (int i = 0;i < n; i++) {
for(int j = 0; j < n; j++) {
cout << "a[" << i << ","<< j << "] = ";
cin >> a[i][j];
}
}
cout << "Enter Vector b:" << "\n";
for(int j = 0; j < n; j++)
{
cout << "b[" << j << "] = ";
cin >> b[j];
}
cout << "Enter the Accuracy = ";
cin >> e;
k = 1e10;
while (k >= e){
p = p + 1;
for (int i = 0; i < n; i++) {
double B = 0;
T[i] = b[i]/a[i][i];
C = a[i][i];
for (int j = 0; j < n; j++) {
if (j!=i)
B += a[i][j] * x[j];
}
x[i] = T[i]-B/C;
}
cout << x[0] << "\n";
cout << x[1] << "\n";
for (int i = 0; i < n; i++)
{
F[i] = 0.0;
for (int j = 0; j < n; j++){
F[i] = F[i] + a[i][j]*x[j];
}
}
for (int i = 0; i < n; i++){
F[i] = F[i]-b[i];
}
k = 0.0;
for (int i = 0;i < n;i++) {
k += F[i] * F[i];
}
k = std::sqrt(k);
}
cout << "Solution :\n";
for (int i = 0; i < n; i++){
cout << "x" << i << "=" << x[i] << "\n";
}
cout << "number of iterations used: " << p <<"\n";
for (int i = 0; i< n; i++){
cout <<"F" << i << "=" << F[i] << "\n";
}
cout << "Final error = " << k << "\n";
return 0;
}

I'm struggeling with Multi-Dimension array input and output, I managed input, however printing doesn't work as planned

I'm playing around to get the grip of multi dimensional arrays.
I have managed to have the array record the input from the user..
I'm trying to use 2 FOR loops to print with the idea that it should print 4 rows of 3 characters each
i know i can solve this if i manually type what to print, but for sure there is a way to have a loop do that for me...
here is the input and output code that i wrote:
cout << "Enter characters" << endl;
for (int i = 0; i < 4; i++)
{
for (int x = 0; x < 3; x++)
{
cin >> charArr[x][i];
}
}
cout << "Printing the array now" << endl;
for (int i = 0; i < 4; i++)
{
for (int x = 0; x < 3; x++)
{
cout << charArr[x][i];
}
cout << endl;
}
i don't understand why some letters are gone and why it doesn't print in order...
where i is row and j is a column. for loop should come like this only.
in this order only we can store the input.
In your case, you are swapping the orders.
Solution :
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> charArr[i][j];
}
}
cout << "Printing the array now" << endl;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
cout << charArr[i][j];
}
cout << endl;
}

How to print one side of the diagonal of an array?

Lets suppose we have a 5 X 5 random array
1 2 3 7 8
4 7 3 6 5
2 9 8 4 2
2 9 5 4 7
3 7 1 9 8
Now I want to print the right side of the diagonal shown above, along with the elements in the diagonal, like
----------8
--------6 5
------8 4 2
---9 5 4 7
3 7 1 9 8
The code I've written is
#include <iostream>
#include <time.h>
using namespace std;
int main(){
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter colums: ";
cin >> columns;
int **array = new int *[rows]; // generating a random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); // random values to array
for(int i = 0; i < rows; i++){ // loop for generating a random array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10; // range of randoms
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){//loop for the elements on the left of
for(int j = columns; j > i; j--){//diagonal including the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for(int i = rows; i >= 0; i++){ //loop for the lower side of
for(int j = 0; j < i - columns; j++){ //the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
return 0;
}
After running the code the shape I get is correct , but the elements do not correspond to the main array. I have no idea what the problem is.
Left side:
for (size_t i = 0; i < rows; i++) {
for(size_t j = 0; j < columns - i; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
Right side:
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < columns; j++) {
if (j < columns - i - 1) cout << "- ";
else cout << vec[i][j] << " ";
}
cout << "\n";
}

C++ Array increment of individual elements

hi i am trying to print the following table using arrays
the first column contains the rating for the movie and the second contains the number of people who rated 1, 2, 3, etc.
i[rating] sum_rating[number of people who have rated 1, 2, 3 and so on]
1 3
2 2
3 4
4 1
5 6
heres what i have tried so far
#include <iostream>
using namespace std;
int main() {
int reviewNum, i, j;
int rating[250], sum_rating[250];
cout << "Enter the number of reviews" << endl;
cin >> reviewNum;
cout << "Enter ratings " << endl;
for (i = 0; i < reviewNum; i++ ) {
cin>> rating[i];
}
for ( i = 0; i <= 5; i++)
sum_rating[i] = 0;
for (int i=0; i< reviewNum; i++){
for(int j=0; j <=5; j++){
if(rating[i]==j){
sum_rating[i] += 1;
}
}
}
cout << "Rating \t Number of people \n";
for ( i = 0; i <= 5; i++)
cout << " " << i+1 << " \t\t" << sum_rating[i] << endl;
return 0;
}
i am somehow getting incorrect output for this program, and my ide is not showing any errors. can someone please explain where its going wrong?
Your primary error is in the loop wherein you compute the statistics:
for (int i=0; i< reviewNum; i++){
for(int j=0; j <=5; j++){
if(rating[i]==j){
sum_rating[i] += 1;
}
}
}
With the structure and indexing as you present, you should be incrementing sum_rating[j], not sum_rating[i]. Of course, part of the problem is that you've made that too complicated. It would be better to avoid the inner loop altogether by simply doing this:
for (int i=0; i< reviewNum; i++){
sum_rating[rating[i]] += 1;
}
You also seem a bit inconsistent about whether ratings go from 1 to 5 or 1 to 6, and you perform no validation of your inputs, but those issues are comparatively minor.

Adding an extra column of data at the end of a 2d array in c

How can I add data from one array and place it as a column on a pre existing array.
example
double array[3][2];
when printed:
3 2
5 5
7 8
and I have another array with other info in it
double arrayb[3]={1,1,1};
i want to run the for loop and be able to print
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
cout << array[i][j];}}
and this is what I want to see:
3 2 1
5 5 1
7 8 1
Try this:
for (int i=0; i<3; i++) {
// You have only two elements in array[i], so the limit should be 2
for (int j=0; j<2; j++) {
// Leave some whitespace before the next item
cout << array[i][j] << " ";
}
// Now print the element from arrayb
cout << arrayb[i] << endl;
}
Seems like the obvious method would be something like:
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++)
std::cout << array[i][j] << '\t';
std::cout << arrayb[i] << '\n';
}