C++ || Adding two matrice arrays - easier way to output? - c++

I'm doing some self study on C++ and have just being doing some chapter on arrays, loops etc. There are a bunch of exercises and the one I'm referencing is quite simple. Initialise two matrices of two rows and three columns.
Output the contents of the matrices (formatted as specified), then perform an addition which is held in a third matrice. Output the third array with the addition done. The code I have works but I'm thinking there's a better way to do the output rather than address each matrice element? I'm thinking of another loop given this is the chapter preceding the exercise, or is this way acceptable?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int amatrix[2][3]=
{
{-5, 2, 8},
{1, 0, 0},
};
int bmatrix[2][3]=
{
{1, 0, 2},
{0, 3, -6},
};
int cmatrix[2][3]=
{
{0, 0, 0},
{0, 0, 0},
};
//add generated matrices
for (int i = 0; i <= 1; i++)
{
for (int j =0; j <= 2; j++)
{
cmatrix[i][j]=amatrix[i][j]+bmatrix[i][j];
}
}
//output to screen - NEED ADVICE FROM HERE
cout << "A= " << endl;
cout << amatrix[0][0] << ", " << amatrix[0][1] << ", " << amatrix[0][2] << endl;
cout << amatrix[1][0] << ", " << amatrix[1][1] << ", " << amatrix[1][2] << endl << endl;
cout << "B= " << endl;
cout << bmatrix[0][0] << ", " << bmatrix[0][1] << ", " << bmatrix[0][2] << endl;
cout << bmatrix[1][0] << ", " << bmatrix[1][1] << ", " << bmatrix[1][2] << endl << endl;
cout << "C= " << endl;
cout << cmatrix[0][0] << ", " << cmatrix[0][1] << ", " << cmatrix[0][2] << endl;
cout << cmatrix[1][0] << ", " << cmatrix[1][1] << ", " << cmatrix[1][2] << endl << endl;
}

cout << amatrix[i][j] in a for loop

Related

Getting large negative numbers printing arrays

Hello Im getting negative valhes when trying to output some variables to the screen.
Ive looked this up and in most cases its an uninitialized variable but I cant find anything wrong.
Saying I have too much code to text ratio but I really dont know how to reiterate so heres some filler.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Input;
int WepType;
string Weapon;
string WeaponType;
int Stats[3];
int AtkStats[4];
cout << "Pick your weapon.\n" << endl;
cout << "{===================================================}\n";
cout << "[ ]\n";
cout << "[ Iron Longsword Sharp Fishing Spear ]\n";
cout << "[ 1 ]\n";
cout << "[ ]\n";
cout << "[ Ornate Hunting Bow ]\n";
cout << "[ ]\n";
cout << "{===================================================}\n";
//Weapon Selection
cin >> Input;
if(Input == 1)
{
WepType = 1;
Weapon = "Iron Longsword";
WeaponType = "OneHanded";
Stats[0] = Stats[0] + 10;
Stats[1] = Stats[1] + 0;
Stats[2] = Stats[2] + 0;
AtkStats[0] = AtkStats[0] + 10;
AtkStats[1] = AtkStats[1] + 0;
AtkStats[2] = AtkStats[2] + 0;
AtkStats[3] = AtkStats[3] + 0;
cout << "Weapon = " << Weapon << endl;
cout << "Weapon Type = " << WeaponType << endl;
cout << "Health = " << Stats[0] << endl;
cout << "Physical = " << AtkStats[0] << endl;
cout << "Light = " << AtkStats[1] << endl;
cout << "Dark = " << AtkStats[2] << endl;
cout << "Temporal = " << AtkStats[3] << endl;
}
return 0;
}
The problem is here:
int Stats[3];
int AtkStats[4];
You should do:
int Stats[3] = {0, 0, 0};
int AtkStats[4] = {0, 0, 0, 0};
Or as BlastFurnace pointed out in the comments (which I forgot about):
int Stats[3] = {}; // Initialize all elements to zero.
int AtkStats[4] = {};
In order to initialize the values. Right now they are just random junk, so when you assign, you get errors.

Print amount of repeated numbers in array c++

I was wondering how to print the amount of repeated numbers from a randomly generated array with an array size of 10, and numbers from 1 - 10.
Example:
Array1: 1 7 6 5 6 7 8 10 9 8
Number of Patterns: 3
(Because it consists of; 2 six, 2 seven, 2 eights)
So far for my code I have done this
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>
#include "Source.h"
#include <algorithm>
using namespace std;
void main()
{
//START OF PROGRAM CODE\\
//Declaritions\\
const int ArraySize = 10;
int arrayMain[ArraySize];
int array1[ArraySize];
int i = 0;
int j = 0;
int k = 0;
//End of Declairations\\
//Store Random Number in Array\\
srand((unsigned)time(0));
for (i = 0; i < 10; i++)
{
arrayMain[i] = (rand() % 10) + 1;
array1[i] = arrayMain[i]; //Copy mainarray to array1
}
for (j = 0; j != ArraySize; j++)
{
sort(array1, array1 + ArraySize); //Sort the array
}
//End of Store Random Number in Array\\
//Program Output\\
cout << "ArrayMain: " << arrayMain[0] << " " << arrayMain[1] << " " << arrayMain[2] << " " << arrayMain[3] << " " << arrayMain[4] << " " << arrayMain[5] << " " << arrayMain[6] << " " << arrayMain[7] << " " << arrayMain[8] << " " << arrayMain[9] << " " << endl;
cout << "Array1: " << array1[0] << " " << array1[1] << " " << array1[2] << " " << array1[3] << " " << array1[4] << " " << array1[5] << " " << array1[6] << " " << array1[7] << " " << array1[8] << " " << array1[9] << " " << endl;
//cout << "Number of Patterns: " << <DATA TO INPUT> << endl;
//END OF PROGRAM CODE\\
}
There's a very simple pattern. You could use an array (I'll be using a vector) of a size equal to the range of possible random numbers
//creates a vector ArraySize big with all elements initialized to 0
std::vector<int> results(ArraySize, 0);
Then, go through your loop and use the random numbers as indexes and increment the values
for(int i = 0; i < 10; i++)
results[(rand() % 10)]++;
Finally, to count how many patterns there are
std::cout << "Number of patterns: ";
std::cout << std::count_if(results.begin(), results.end(), [](int i){return i > 1;});
std::cout << std::endl;

How do I store user input into a multidimensional array? I'm working with a 3D array

I understand how Arrays work despite their dimensions, but I can't make it come together in code without this basic information. As you can tell I tried multiple ways starting with line 13, but they all failed.
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main()
{
int MasterArray [3][3][2]; // Declaration of the Array
int one = 0, two = 0, three = 0; // Declaration of Variables
cout << "Would you like to edit class 1 or 2?" << endl ; //Ask for input
cin >> one; // store input in the variable named one
one -=1; // Since c++ is 0 based input - 1 = one
MasterArray[3][3][2] = {{one, two, three}, {one, two, three}, {one, two}};
// Above line attempt to store variable one in array
// Rinse and repeat this process for lower lines, but storing is all that matters.
cout << "Which student account would you like to access 1, 2, or 3?";
cin >> two;
two -= 1;
MasterArray[3][3][2] = [one][two][three];
cout << "Which grade would you like to edit 1, 2, or 3?";
cin >> three;
three -= 1;
MasterArray[3][3][2] = [one][two][three];
cout << MasterArray[one][two][three];
return 0;
}
Multi-dimensional arrays in c++ are a bit more complicated and require extra work so using a multidimensional vector would probably be better for you.
vector<vector<vector<int> > > yourVector;
for(int i = 0; i<3; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k <3; k++)
yourVector[i][j][k] = i + j +k
cout << yourVector[1][2][3]; //prints 6
I figured out my answer last night. This is the updated code if anyone is interested. Thanks for trying to help everyone.
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int one = 0, two = 0, three = 0, x = 0;
int MasterArray [3][3][2] = {three, two, one};
float A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, A6 = 0;
for (int i = 0;i <18; i++)
{
cout << "Enter a class number 1 or 2 : ";
cin >> one;
one -= 1;
cout << "Enter a student number 1, 2, or 3: ";
cin >> two;
two -= 1;
cout << "Enter the test number 1, 2, 3 : ";
cin >> three;
three -= 1;
cout << "Enter a grade for test number " << three << ": ";
cin >> x;
MasterArray[three][two][one] = x;
cout <<"Class number: " << one + 1 << " Student number: " << two + 1 << " Test Number: " << three + 1 << " Grade: " << MasterArray[three][two][one] << endl << "\n";
}
//Math
cout.precision(2), cout << fixed;
A1 = MasterArray[0][0][0]*MasterArray[0][0][1]*MasterArray[0][0][2]/3;
A2 = MasterArray[0][1][0]*MasterArray[0][1][1]*MasterArray[0][1][2]/3;
A3 = MasterArray[0][2][0]*MasterArray[0][2][1]*MasterArray[0][2][2]/3;
A4 = MasterArray[1][0][0]*MasterArray[1][0][1]*MasterArray[1][0][2]/3;
A5 = MasterArray[1][1][0]*MasterArray[1][1][1]*MasterArray[1][1][2]/3;
A6 = MasterArray[1][2][0]*MasterArray[1][2][1]*MasterArray[1][2][2]/3;
cout << "Class" << setw(10) << "Student" << endl;
cout << "Number" << setw(8) << "Number" << setw(8) <<"Grade 1" << setw(8) <<"Grade 2" << setw(8) <<"Grade 3" << setw(8) << "Average" << endl;
// class 1
cout << "1" << setw(8) << "1" << setw(8) << MasterArray[0][0][0] << setw(8) << MasterArray[0][0][1] << setw(8) << MasterArray[0][0][2] << setw(12) << A1 << endl;
cout << setw(9) << "2" << setw(8) << MasterArray[0][1][0] << setw(8) << MasterArray[0][1][1] << setw(8) << MasterArray[0][1][2] << setw(12) << A2 << endl;
cout << setw(9) << "3" << setw(8) << MasterArray[0][2][0] << setw(8) << MasterArray[0][2][1] << setw(8) << MasterArray[0][2][2] << setw(12) << A3 << endl;
cout << "--------------------------------------------------------------------------------";
// class 2
cout << "2" << setw(8) << "1" << setw(8) << MasterArray[1][0][0] << setw(8) << MasterArray[1][0][1] << setw(8) << MasterArray[1][0][2] << setw(12) << A4 << endl;
cout << setw(9) << "2" << setw(8) << MasterArray[1][1][0] << setw(8) << MasterArray[1][1][1] << setw(8) << MasterArray[1][1][2] << setw(12) << A5 << endl;
cout << setw(9) << "3" << setw(8) << MasterArray[1][2][0] << setw(8) << MasterArray[1][2][1] << setw(8) << MasterArray[1][2][2] << setw(12) << A6 << endl;
return 0;
}

C++ array beginner

I have this code that I have been working on for fun, it is as basic as it gets, becuase I am a beginner, and it works fine, but I can't seem to be able to figure out how to make it show the least and the most amount of pancakes ate. Thank you a lot in advance.
#include <iostream>
using namespace std;
int main(){
int pancakes[10];
int x,i;
cout << "Hello user!" << endl;
cout << endl;
cout << "Please enter how many pancakes did each of the 10 people eat:" << endl;
cout << endl;
for (i=0;i<10;i++ ){
cin >> x;
pancakes[i]=x;
}
cout << "1st person ate" << " " << pancakes[0] << " " << "pancakes" << endl;
cout << "2nd person ate" << " " << pancakes[1] << " " << "pancakes" << endl;
cout << "3rd person ate" << " " << pancakes[2] << " " << "pancakes" << endl;
cout << "4th person ate" << " " << pancakes[3] << " " << "pancakes" << endl;
cout << "5th person ate" << " " << pancakes[4] << " " << "pancakes" << endl;
cout << "6th person ate" << " " << pancakes[5] << " " << "pancakes" << endl;
cout << "7th person ate" << " " << pancakes[6] << " " << "pancakes" << endl;
cout << "8th person ate" << " " << pancakes[7] << " " << "pancakes" << endl;
cout << "9th person ate" << " " << pancakes[8] << " " << "pancakes" << endl;
cout << "10th person ate" << " " << pancakes[9] << " " << "pancakes" << endl;
return 0;
}
Since you are a beginner, I will put a simple solution using a loop.
int max = 0;
for(i = 0; i < 10; i++) {
if(pancakes[i] > max) max = pancakes[i];
}
cout << "Most amount of pancakes eaten by a single person: " << max << endl;
You can use min_element and max_element from the standard library to do this:
#include <algorithm>
cout << "The smallest number of pancakes was " << *min_element(pancakes, pancakes + 10) << endl;
cout << "The largest number of pancakes was " << *max_element(pancakes, pancakes + 10) << endl;
Firstly, instead of having around 10 cout's, you can use a loop to print them :
for (i=0;i<10;i++ ){
if(i==0)
cout <<i+1<< "st person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==1)
cout << i+1<<"nd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==2)
cout << i+1<<"rd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else
cout <<i+1<< "th person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
}
Secondly, you can directly enter values into your array, no need for an intermediate variable x:
for (i=0;i<10;i++ ){
cin >> pancakes[i];
}
For your max and min problem, take two variables, say - max and min. Initialise them to any arbitrary smallest (say 0, if you are not dealing with negative numbers) and largest value (say, INT_MAX) respectively. Alternatively, you can initialise them to the first element of your array.
For finding max and min, you can traverse the entire array, while checking if the elements are greater or lesser than your max and min variables. If they are, then assign them to your variables:
for (i=0;i<10;i++ ){
if(pancakes[i]>max)
max = pancakes[i];
if(pancakes[i]<min)
min = pancakes[i];
}
You can add std::cout inside for loop like below,
for (int i = 0; i < 10; i++ )
{
std::cin >> pancakes[i];
std::cout << i+1 <<"st person ate" << " " << pancakes[i] << " " << "pancakes" << std::endl;
}
int maxpancakes = pancakes[0];
int minpancakes = pancakes[0];
for(int i = 0; i < pancakes.length(); i++ )
{
if( pancakes[i] < minpancakes )
minpancakes = pancakes[i];
if( pancakes[i] > maxpancakes )
maxpancakes = pancakes[i];
}
std::cout << "The smallest pan cake had is :" << minpancakes << std::endl;
std::cout << "The max pan cake had is :" << maxpancakes << std::endl;

Getting a element in a structure array

This program choose any bin of same number of parts from 10 bins. When the program chose one, it ask if we want to either add or remove parts from that specific bin. How can I get a element from the structure array.
//Structure
struct Inventory
{
char description[35];
int num;
};
//Function Prototypes.
void choiceMenu(Inventory[], int);
void AddParts(Inventory[], int);
void RemoveParts(Inventory[]);
int main()
{
char election;
int choice;
const int Number_Bins = 10;
Inventory parts[Number_Bins] = {
{"Valve", 10},
{"Bearing", 5},
{"Bushing", 15},
{"Coupling", 21},
{"Flange", 7},
{"Gear", 5},
{"Gear Housing", 5},
{"Vacuum Gripper", 25},
{"Cable", 18},
{"Rod", 12}
};
Is there other way to do it without putting the elements of the arrays from 0 to 9. like trying to do it with a accumulator. how can I take a specific element from the array.
void choiceMenu(Inventory bin[], int z)
{
cout << " Inventoy Bins\n";
cout << " = = = = = = = = \n";
cout << " *Choose the part of your preference.\n";
cout << " 1. Valve" << bin[0].num << endl;
cout << " 2. Bearing. Currently Number of Bearing = " << bin[1].num << endl;
cout << " 3. Bushing. Currently Number of Bushing = " << bin[2].num << endl;
cout << " 4. Coupling. Currently Number of Coupling = " << bin[3].num << endl;
cout << " 5. Flange. Currently Number of Flange = " << bin[4].num << endl;
cout << " 6. Gear. Currently Number of Gear = " << bin[5].num << endl;
cout << " 7. Gear_Housing" << bin[6].num << endl;
cout << " 8. Vacuum_Gripper" << bin[7].num << endl;
cout << " 9. Cable. Currently Number of Cable = " << bin[8].num << endl;
cout << " 10. Rod. Currently Number of Rod = " << bin[9].num << endl;
cout << " 11. Choose 11 to quit the Program" << endl;
}
I'm thinking you're asking how to pass one of the array elements to a function that adds or removes parts. You can do that by using a reference variable (http://www.cprogramming.com/tutorial/references.html).
For example, in your case, this function would remove a part:
void RemovePart(Inventory& part)
{
if (part.num > 0)
part.num -= 1;
cout << part.description << " now has " << part.num << " parts." << endl;
}
Then you can call that function with an array element. For example, this removes a Bearing:
RemovePart(bin[1]);