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;
}
Related
The code is supposed to detect the common elements of two sets of 5, which it does but whenever there is nothing in common, for some reason it cannot detect it and output "None". What am I supposed to do? I've tried setc.empty() but it was also an error, and now I'm trying to make setc[0] equal to -727, a number that shouldn't be outputted anyways and yet there's still an error.
#include <set>
#include <iostream>
#include <array>
using namespace std;
int main ()
{
int seta[] = {1, 2, 3, 4, 5};
int setb[] = {3, 4, 5, 6, 7};
cout << "Enter 5 numbers for Set A: " ;
for(int i=0; i<5; i++){
cin >> seta[i];
}
cout <<endl << "Enter 5 numbers for Set B: ";
for(int i=0; i<5; i++){
cin >> setb[i];
}
int i, j, k=0, x, common;
int setc[0]=-727;
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(seta[i]==setb[j])
{
common = 0;
for(x=0; x<k; x++)
{
if(seta[i]==setb[x])
common++;
}
if(common==0)
{
setc[k] = seta[i];
k++;
}
}
}
}
if(setc[0]=-727)
cout<< "None" << endl;
else
for(i=0; i<k; i++)
cout << setc[i] << " " << endl;
cout << endl;
return 0;
}
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);
}
i wrote a simple sorting code on pass by reference. here i am passing an array to function and then performing the sorting operation. after passing the array i am printing the entire array as entered by user then performing the sorting operation(in descending order) but after sorting when i print the sorted array i get an output that the array index '0' contains the value '41'. if i enter the numbers less than '41' then the sorted array is displayed as '41',and then the other numbers in sorted manner. Please explain why i am getting such an output.
#include<iostream>
using namespace std;
int sort_array(int *p);
int main() {
int arr[10];
for (int i=0; i<10; i++) {
cout << "enter " << (i+1) << " value:";
cin >> arr[i];
cout << "\n";
}
sort_array(arr);
return 0;
}
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:"<<p[0];
cout<<"\n";
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (p[j] < p[j+1]) {
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << p[0];
}
It appears that you are trying to do a bubble sort on your array in sort_array(), but the logic is wrong. Try using this code instead:
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:" << p[0];
cout << "\n";
for (int i=0; i < 10; i++) {
for (int j=1; j < (10-i); j++) {
if (p[j-1] > p[j]) {
c = p[j-1];
p[j-1] = p[j];
p[j] = c;
}
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout<<p[0];
}
The problem is in your sorting. j is from 0 to 9, and than you access p[j+1] when j = 9, p[10] is outside your array boundaries.
So fix your following part to proper sorting.
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
Clarification: the code above is the problematic part of the original code posted. This is NOT the fixed sorting. That is the part to be fixed.
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
the sorting works fine once i changed the limit of inner loop. the problem was accessing the array index [10] but i had it declared till index [9].
The user enteres a number which is put in an array and then the array needs to be orinted backwadrds
int main()
{
int numbers[5];
int x;
for (int i = 0; i<5; i++)
{
cout << "Enter a number: ";
cin >> x;
numbers[x];
}
for (int i = 5; i>0 ; i--)
{
cout << numbers[i];
}
return 0;
}
You're very close. Hope this helps.
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int numbers[5];
/* Get size of array */
int size = sizeof(numbers)/sizeof(int);
int val;
for(int i = 0; i < size; i++) {
cout << "Enter a number: ";
cin >> val;
numbers[i] = val;
}
/* Start index at spot 4 and decrement until k hits 0 */
for(int k = size-1; k >= 0; k--) {
cout << numbers[k] << " ";
}
cout << endl;
return 0;
}
You are very close to your result but you did little mistakes, the following code is the correct solution of the code you have written.
int main()
{
int numbers[5];
int x;
for (int i = 0; i<5; i++)
{
cout << "Enter a number: ";
cin >> numbers[i];
}
for (int i = 4; i>=0; i--)
{
cout << numbers[i];
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//get size of the array
int arr[1000], n;
cin >> n;
//receive the elements of the array
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
//swap the elements of indexes
//the condition is just at "i*2" be cause if we exceed these value we will start to return the elements to its original places
for (int i = 0; i*2< n; i++)
{
//variable x as a holder for the value of the index
int x = arr[i];
//index arr[n-1-i]: "-1" as the first index start with 0,"-i" to adjust the suitable index which have the value to be swaped
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = x;
}
//loop for printing the new elements
for(int i=0;i<n;i++)
{
cout<<arr[i];
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
//print numbers in an array in reverse order
int myarray[1000];
cout << "enter size: " << endl;
int size;
cin >> size;
cout << "Enter numbers: " << endl;
for (int i = 0; i<size; i++)
{
cin >> myarray[i];
}
for (int i = size - 1; i >=0; i--)
{
cout << myarray[i];
}
return 0;
}
of course you can just delete the cout statements and modify to your liking
this one is more simple
#include<iostream>
using namespace std;
int main ()
{
int a[10], x, i;
cout << "enter the size of array" << endl;
cin >> x;
cout << "enter the element of array" << endl;
for (i = 0; i < x; i++)
{
cin >> a[i];
}
cout << "reverse of array" << endl;
for (i = x - 1; i >= 0; i--)
cout << a[i] << endl;
}
answer in c++. using only one array.
#include<iostream>
using namespace std ;
int main()
{
int array[1000] , count ;
cin >> count ;
for(int i = 0 ; i<count ; i++)
{
cin >> array[i] ;
}
for(int j = count-1 ; j>=0 ; j--)
{
cout << array[j] << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ()
{
int array[10000];
int N;
cout<< " Enter total numbers ";
cin>>N;
cout << "Enter numbers:"<<endl;
for (int i = 0; i <N; ++i)
{
cin>>array[i];
}
for ( i = N-1; i>=0;i--)
{
cout<<array[i]<<endl;
}
return 0;
}
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};
^^^^^^