I was testing after some years without code, to make a mini bingo test...
In the end, I had:
#include <iostream>
using namespace std;
int main()
{
int cartela[5][3] = { {1, 2, 3}, {6,7,8}, {11,12,13}, {14,15,16}, {17,18,19} } ;
int sorteado[8];
int detectado[5];
cout << "Insira os 8 numeros sorteados: ";
cin >> sorteado[0] >> sorteado[1] >> sorteado[2] >> sorteado[3] >> sorteado[4] >> sorteado[5] >>sorteado[6] >> sorteado[7];
for (int tsort=0; tsort<9; tsort++) {
for (int i=0; i<6; i++) {
for (int j=0; j<4; j++) {
if (cartela[i][j] == sorteado[tsort]) {
detectado[i]++;
}
}
}
}
cout << "cartela 1: " << detectado[0]<<"\n";
cout << "cartela 2: " << detectado[1]<<"\n";
cout << "cartela 3: " << detectado[2]<<"\n";
cout << "cartela 4: " << detectado[3]<<"\n";
cout << "cartela 5: " << detectado[4]<<"\n";
return 0;
}
or pastebin:http://pastebin.com/TLYAZTtE
And if you note, the objective is get the number of numbers that the user typed that is in the mounted games (cartela[5][3]).
And, the result is reached only in the game 2 and 3.
In the rest, the result is INCREDIBLE. LOL
Anyone may help me find what was my error?
THANKS!
Your indexes are wrong. For example, look at this:
int sorteado[8];
i.e. an index of 8 values, but in the for loop:
for (int tsort=0; tsort<9; tsort++)
you are looping 9 elements, from 0 to 8. To correct it:
for (int tsort=0; tsort< 8; tsort++) {
for (int i=0; i< 5; i++) {
for (int j=0; j< 3; j++) {
if (cartela[i][j] == sorteado[tsort]) {
detectado[i]++
}
}
}
Now, really, what is your question?
UPDATED
You forgot to initialize "detectado", so, update your code to:
int detectado[5] = {0};
^^^^^^
Related
So the question requires us to check if the entered elements in an array form a palindrome or not.
For eg, if an array has 134, 21, 12, 431, the output should declare that this is a palindrome.
I tried my best to keep my program simple and the logic used looks right to me. But it still isn't giving me the correct output. For eg, I input the size as 4 and then entered 134, 21, 12, 431. As per my code, I think it should print "It is a palindrome." but instead there's no ouput on the console.
would be great if somebody can help me with this.
#include <iostream>
using namespace std;
int main()
{
int size;
cout << "Enter the size of array: " << endl;
cin >> size;
int arr[size];
cout << "Enter the element: " << endl;
for (int i=0; i<size; i++)
{
cin >> arr[i];
}
int arr2[size/2];
for (int i=0; i<((size/2)-1); i++)
{
int reverse=0;
while (arr[i]!=0)
{
int rem;
rem = arr[i]%10;
reverse=((reverse*10)+rem);
arr[i]=arr[i]/10;
}
arr2[i]=reverse;
}
for (int i=0; i<((size/2)-1); i++)
{
if(arr2[i]==arr[size-1-i])
{
if(i==(size/2)-1)
{
cout << "It is a palindrome." << endl;
}
}
else
{
cout << "Array is not a palindrome." << endl;
}
}
}
You can check whether the count of non-repeated couples is 1 or 0.
bool isPalindrome(std::vector<int>& vec) {
int max = *(std::max_element(vec.begin(), vec.end()));
std::vector<int> tmp(max + 1, 0);
for (int i = 0; i < vec.size(); ++i) {
++tmp[vec[i]];
}
int odd = 0;
for (int i = 0; i < tmp.size(); ++i) {
if (tmp[i] % 2 == 1) {
++odd;
}
}
return (odd == 1 || odd == 0);
}
having a trouble with a condition which tells if an element of a multidimensional array
as example arr[row][col] is exactly arr[col][row] then automatically assign the value of arr[col][row] to the same value of arr[row][col] without letting user enter it manually
That should be an example of the output
example[4][4] =
{
// 0 1 2 3
{ 0, 10, 15, 18}, // 0
{ 10, 0, 20, 14}, // 1
{ 15, 20, 0, 90}, // 2
{ 18, 14,90, 0}, // 3
here's my code
` int size;
int arr[size][size];
cout<<"Choose size of your multidimensional array [matrix]: ",cin>>size;
cout<<"Now enter your data [Respectively] \n";
for(int d=0 ; d<size ; d++)
{
for(int j=0; j<size; j++)
{
if (d==j)
{
arr[d][j]=0 ;
}
else if(arr[d][j]!=0 ) //there should be the problem
{
arr[j][d]=arr[d][j];
}
else
{
cin>>arr[d][j]; // Filling matrix
}
}
} `
Here is a Minimum Working Example (MWE) that should solve your issues:
#include <iostream>
using namespace std;
int main()
{
cout << "Choose size of your multidimensional array [matrix]: ";
int size;
cin >> size;
int arr[size][size];
cout << "Now enter your data [Respectively] \n";
for(int d=0; d<size; d++)
{
for(int j=d+1; j<size; j++)
{
if (d==j)
{
arr[d][j] = 0;
}
else if(j<d) // here laid the problem
{
arr[d][j] = arr[j][d];
}
else
{
cin >> arr[d][j]; // Filling matrix
}
}
}
// print matrix
for(int d=0; d<size; d++) {
for(int j=0; j<size; j++)
cout << arr[d][j] << " ";
cout << '\n';
}
return 0;
}
This below is even cleaner, gets rid of the conditional inside the inner loop, thank you #ThomasSablik
#include <iostream>
using namespace std;
int main()
{
cout << "Choose size of your multidimensional array [matrix]: ";
int size;
cin >> size;
int arr[size][size];
cout << "Now enter your data [Respectively] \n";
for(int d=0 ; d<size ; d++)
{
arr[d][d]=0;
for(int j=d+1; j<size; j++)
{
cin >> arr[d][j];
arr[j][d]=arr[d][j];
}
}
// print matrix
for(int d=0; d<size; d++) {
for(int j=0; j<size; j++)
cout << arr[d][j] << " ";
cout << '\n';
}
return 0;
}
I want to find the factors of a product by using arrays to check whether they equal it or not
the values do not print
here is the code
#include <iostream>
using namespace std;
int main() {
int arr[5] = { 1,3,5,7,2 };
int arr1[5] = { 0,6,5,4,9 };
int X;
cout << "Please enter X:"; cin >> X;
for (int i = 0, j = 0; i < 5 && j < 5; ++i, ++j) {
if (arr[i]*arr[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
Use this nested loops
for (int i = 0; i < 5 ;++i) {
for(int j=0 ;j<5;++j){
if (arr[i]*arr1[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 6 years ago.
So the requirements for this program is to be able to increment arrays of the same size (size from 5 to 15 indexes) and increment each element in the array by one using for and while loops. The last task is to take values from the first array and put them in reverse order and assign them to the second array.
So everything works as normal, and the program rejects invalid inputs and does not go into an infinite loop. However, the program accepts some inputs that are not wanted.
For example, I would input something like '12 a' or '7 asdfkla;j lasnfg jasklgn asfg' and it would go through. It is interesting too because the code registers only 12 or 7 and completely ignores the rest. I think it is because once it hits a non-integer character, it would stop ignore the rest.
Why is it ignoring the rest of the input? And is there a way to catch this error from going through?
Also, if you see anything that catches your eye, feel free to critique c: I am always looking to improving.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int x;
int j = 0;
bool not_valid = true;
system("color f");
cout << "Program will ask for an input for the size of an array.\n"
<< "With the array size defined, program will generate semi-\n"
<< "true random integers from 0 to 8. First array will then\n"
<< "be assigned to the second in reverse (descending) order.\n\n";
do {
cout << "Enter array size (0 - 15): ";
cin >> x;
if (x >= 5 && x <= 15) {
not_valid = false;
cout << "\nArray size: " << x << endl;
}
else {
cout << "Invalid input.\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (not_valid);
int *arr0;
int *arr1;
arr0 = new int[x];
arr1 = new int[x];
for (int i = 0; i < x; i++) {
arr0[i] = rand() % 9;
}
for (int i = 0; i < x; i++) {
arr1[i] = rand() % 9;
}
cout << "\nARRAY 0 (unmodified, for):\n";
for (int i = 0; i < x; i++) {
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 0 (modified, for):\n";
for (int i = 0; i < x; i++) {
arr0[i]++;
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 1 (unmodified, while):\n";
for (int i = 0; i < x; i++) {
cout << arr1[i] << "\t";
}
cout << "\n\nARRAY 1 (modified, while):\n";
while (j < x) {
arr1[j]++;
cout << arr1[j] << "\t";
j++;
}
int second = x - 1;
for (int i = 0; i < x; i++) {
arr1[second] = arr0[i];
second--;
}
j = 0;
cout << "\n\nARRAY 1 (array 0, descending):\n";
while (j < x) {
cout << arr1[j] << "\t";
j++;
}
cout << endl << endl;
system("pause");
return 0;
}
Take input in string and then check if it's a number or not.
Example:
#include<iostream>
#include<sstream>
#include <string>
using namespace std;
int main()
{
string line;
int n;
bool flag=true;
do
{
cout << "Input: ";
getline(cin, line);
stringstream ss(line);
if (ss >> n)
{
if (ss.eof())
{
flag = false;
}
else
{
cout << "Invalid Input." << endl;
}
}
}while (flag);
cout << "Yo did it !";
}
I'm new to programming and am struggling with the idea of setting up a two dimensional array that is 5x5 and allows for the input of the numbers 0-10. I've scratched down a few things but need some advice or guidance. The book and youtube videos can't help me any further.
#include <iostream>
using namespace std;
int main()
{
int numbers[5][5];
for (int i = 0; i < 5; i++)
{
cout << "Please enter the five scores for Contestant #1: " << i << endl;
cin >> numbers[i]
}
int *array;
cout << "Please enter the contestants 5 scores: ";
int n;
cin >> n;
array = new int[n];
for (int i = 0; i < n; i++)
try this:
include
using namespace std;
int main()
{
int numbers[5][5];
//Input Data
for (int i = 0; i < 5; i++)
{
for (int j = 0; j<5; j++)
{
cout << "Please enter Contestant # " << i <<"score for Test # "<<j<<" :"<<endl;
cin>>numbers[i][j];
}
}
//Print Data
for (int i = 0; i < 5; i++)
{
for (int j = 0; j<5; j++)
{
cout << "Contestant # " << i <<"score for Test # "<<j<<" :"<< numbers[i][j]<<endl;
}
}
return 0;
}