Strange C++ matrix multiplication/initialization - c++

I'm using Cygwin, Windows Vista, Norton anti-virus (someone asked me about this before). I previously asked a question about strange C++ behavior that no one could answer. Here's another. Simple matrix multiplication exercise. This form below gives strange (and wrong) results:
#include <iostream>
#include <string>
#include<cmath>
using namespace std;
int main(int argc, char* argv[])
{
int A[3][3]={{1,5,0},{7,1,2},{0,0,1}};
int B[3][3]={{-2,0,1},{1,0,0},{4,1,0}};
int D[3][3];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
D[i][j]+=A[i][k]*B[k][j];
}
}
}
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<D[i][j]<<"\n";
}}
return 0;
}
BUT a very small change gives correct results: (all I've done is move the initialized matrices outside main() ).
#include <iostream>
#include <string>
#include<cmath>
using namespace std;
int A[3][3]={{1,5,0},{7,1,2},{0,0,1}};
int B[3][3]={{-2,0,1},{1,0,0},{4,1,0}};
int D[3][3];
int main(int argc, char* argv[])
{
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
D[i][j]+=A[i][k]*B[k][j];
}
}
}
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<D[i][j]<<"\n";
}}
return 0;
}

You forgot to initialize the array D to 0 in your first case. This is automatically done when the array is global, but not when it is local (simplified, but explains the behavior).

Related

using std::async appear error: "No viable overloaded '='"

#include <future>
#include <thread>
#include <iostream>
#include <mutex>
void f(int n){
for(int i=0; i<n; i++){
printf("Thread[%d]: %d\n",std::this_thread::get_id(), i);
}
}
int main(int argc, const char * argv[]) {
int thCNT = 5;
std::future<int> mTH[thCNT];
for(int i=0; i<thCNT; i++){
mTH[i] = std::async(f, i+5);
}
for(int i=0; i<thCNT; i++){
mTH[i].get();
}
return 0;
}
code like above
xcode show an error like this, I dont know how to solve it.
I copy the code from an instruction, I hope to know if there is anything different between C++20 and C++11, since the instruction is made by C++11.
273K is right.
I should return int when use future like below:
int f(int n){
for(int i=0; i<n; i++){
printf("Thread[%d]: %d\n",std::this_thread::get_id(), i);
}
return 0;
}

A problem of ending the implementation of the code

this code works well for all samples, but after all samples are finished, a problem occurs. I don’t know what happens and the program crashes. Is there a problem with this code?
i have this problem when i use strings arrays usualy can it be the problem?
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
long long t,n;
int a[1000];
string str[1000];
int main()
{
cin>>t;
for(int r=1;r<=t;r++){
cin>>n;
int maxi=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>maxi)maxi=a[i];
};
//input first value
maxi=maxi+3;
for(int r1=0;r1<maxi;r1++){
str[1][r1]=(rand()%26)+'a';
}
for(int i=0;i<maxi;i++){
cout<<str[1][i];
}
cout<<endl;
//
for(int k=2;k<=(n+1);k++){
int w;
for(w=0 ; w<=a[k-1];w++){
str[k][w]=str[k-1][w];
};
for(int l=w-1;l<maxi;l++){
str[k][l]=(rand()%26)+'a';
};
for(int i=0;i<maxi;i++){
cout<<str[k][i];
}
cout<<endl;
}
}
return 0;
}
You are using elements of strings without allocating them.
Allocate elements by inserting
for(int i=1;i<=(n+1);i++){
str[i].resize(maxi);
}
just after
maxi=maxi+3;

Different results in CodeBlocks and Visual Studio 2019

I used CodeBlocks to code this program:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
ifstream is;
is.open("game.inp");
int n,a[100];
is>>n;
for (int i=0; i<n; i++)
is>>a[i];
is.close();
int game[100];
int kt=0;
for (int i=0; i<n; i++)
{
for (int j=3; j<a[i]+1; j++)
{
if ((a[i]%j)==0)
{
int *x = find(begin(game),end(game),j); //ktra uoc hien tai da co trong mang hay chua, k co thi ms them
if (x==end(game))
{
game[kt]=j;
kt++;
}
}
}
}
int kq=0;
for (int i=0; i<kt; i++)
{
int d=0;
for (int j=0; j<n; j++)
{
if ((a[j]%game[i])==0)
d++;
}
if (d>kq)
kq=d;
}
ofstream o;
o.open("game.out");
o<<kq;
o.close();
return 0;
}
and the result was not correct. Then I decided to copy these codes to Visual Studio 2019 and it gave me the correct result. I don't know what happened. I copied the same codes from CodeBlocks to VS and the results were completely different.
Welcome to the wonderful world of undefined behavior.
You use the contents of the array game before it's initialized. Local variables are not initialized, their contents is indeterminate and using indeterminate values lead to undefined behavior.
If you want the array to be initialized to all zeroes you need to do it explicitly:
int game[100] = { 0 };

C++ basic coding issue

#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
I want to see the output is about the integers inclusive between a and b, but after entering two numbers, it shows no output..
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
First you can't use endl in cin
Second you wrote ++i inside your for loop which will increase value of by i means value of will become 1 from 0.
Therefore the condition will never be true as value of b is 0.
THE CORRECT WAY
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b;
for(int i=a;i<=b;i++)
cout<<i<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 0, b = 0;
cin >> a;
cin >> b;
for (int i = a; i <= b; i++)
cout << i << endl;
return 0;
}
EDIT: I removed something as it wasn't true :P Silly me.
Also 'endl' doesn't work with cin :)
The code is wrong because you have already got a and b equal to 0,and after that you are taking a and b as inputs.
If you wanna take them as inputs you should write int a,b. NOT int a=0,b=0

C++ Vector Elements Count

In C++, using the vector header, how do I find the number of elements?
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
system("pause");
return 0;
primer(1000);
}
int primer(int max){
vector<int> a;
a[1]=2;
for (int i=2;i<=max;i++){
bool prime=true;
for (int ii=1;ii<=#a;ii++) {
if i/a[ii]==math.floor(i/a[ii]) {
prime=false;
}
}
if prime==true {
a[#a+1]=i;
}
}
for (i=1;i<=#a;i++) {
cout << a[i]);
}
}
}
I originally wrote the code
for lua, and this is my attempt to translate it to C++. I would appreciate specifics, for example, a specific replacement for a bad line. I tried to replace #a with a.size, but it didn't work.
Revised:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
primer(5);
system("pause");
return 0;
}
int primer(int max){
vector<int> a;
a[1]=2;
for (int i=2;i<=max;i++){
bool prime=true;
for (int ii=0;ii<a.size();ii++) {
if (i/a[ii]==floor(i/a[ii])) {
prime=false;
}
}
if (prime==true) {
a.push_back(i);
}
}
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
}
}
It crashes without running. For what reason is this?
a.size().
I would recommend using some sort of reference material, e.g. http://cplusplus.com/reference/stl/vector/.
To answer your immediate question:
a.size(); // use size as a function
But there are several other things wrong with your code:
vector<int> a;
a[1]=2;
Ordinarily you need to set the size of a beforehand, since C++ must allocate space for it. You can use push_back() though, which will incrementally add space as needed.
Also, C++ arrays start counting at 0:
for (int ii=1;ii<=#a;ii++) {
This should be
ii = 0
And since arrays start at 0, they end at size() - 1, not size().
for( int ii = 0; ii < a.size(); ++ii )
C and C++ array indexes start at zero and end at size-1, so you need to compare less-than, not less-than-or-equal-to. vector follows the same rule.
Another obvious problem that needs pointing out:
int main()
{
system("pause");
return 0;
primer(1000);
}
Your function is never going to be called. Your app will exit when main returns.
a[#a+1]=i;
changed to use size() becomes:
a[ a.size() + 1 ] = i;
This is syntactically correct but guaranteed wrong. It should be:
a.push_back(i);
Read the API referenced by Oli.