Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5932005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:46:28+00:00 2026-05-22T14:46:28+00:00

I have no idea what’s wrong with my code … It always return zeros

  • 0

I have no idea what’s wrong with my code … It always return zeros in all the elements. A hint of where is the problem would be great 🙂

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

int nGlobalCount = 0;
int thread_index = 0;
int num_of_thr=5;

int a[4][4], b[4][4], c[4][4];
int i, j, k;

struct v {
    int i; /*row*/
    int j; /*column*/
};

DWORD ThreadProc (LPVOID lpdwThreadParam ) {
    //
    struct v *input = (struct v *)lpdwThreadParam;
    int avg=4*4/num_of_thr;
    int count=0;

    for(int i = 0; i <= 3 ; i++) {
        for(int j = 0; j <= 3; j++) {
            int sum=0;
            for ( k = 0 ; k <= 3; k++) {
               sum=sum+((a[input->i][k])*(b[k][input->j]));
                c[input->i][input->j]=sum;
                count++;
            }
        }
    }

    //Print Thread Number
    //printf ("Thread #: %d\n", *((int*)lpdwThreadParam));
    //Reduce the count
    return 0;
}

int main() {
    //    int x=0;
    cout<<"enter no of threads : ";
    cin>>num_of_thr;
    DWORD ThreadIds[num_of_thr];
    HANDLE ThreadHandles[num_of_thr];
    //struct v {
    //    int i; /*row*/
    //    int j; /*column*/
    //};

    struct v data[num_of_thr];
    int i , j , k;

    for ( int i = 0 ; i <= 3; i++) {
        for (int j = 0 ; j <= 3 ; j++) {
            a[i][j] = rand() % 10;
            b[i][j] = rand() % 10;
            c[i][j] = 0;
        }
    }

    for(int i = 0; i < num_of_thr/2; i++) {
        for(int j = 0; j < num_of_thr/2; j++) {
            data[thread_index].i = i;
            data[thread_index].j = j;

            ThreadHandles[thread_index] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadProc, &data[thread_index], 0,&ThreadIds[thread_index]);

            thread_index++;
        }
    }

    WaitForMultipleObjects(num_of_thr, ThreadHandles, TRUE, INFINITE);
    cout<<"The resultant matrix is "<<endl;
    for ( i = 0 ; i < 4; i++) {
        for ( j = 0 ; j < 4 ; j++)
            cout<<c[i][j]<<" ";
        cout<<endl;
    }
    for (int i=0; i<num_of_thr; i++)
        CloseHandle(ThreadHandles[i]);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:46:29+00:00Added an answer on May 22, 2026 at 2:46 pm

    A few things I found while poking about in addition to the other issues noted previously:

    • What are you compiling this with? With VC++ 2010 it “works”, as in it outputs non-zeroes, although it complains about the DWORD ThreadIds[num_of_thr]; array declaration with a non-constant array size (I just made num_of_thr a constant and commented out the cin to test it quickly).
    • Are you sure you are inputting a valid number of threads with cin >> num_of_thr; For example, if num_of_thr was 0 this would explain the zeroes output. A simple cout here for num_of_thr would be useful.
    • In your data initialization loop starting with for(int i = 0; i < num_of_thr/2; i++) { you are not correctly counting threads which will result in an array underflow or overflow. For example, if num_of_thr is 5 then num_of_thr/2 is 2 which results in initializing only the elements 0..3 leaving the last element uninitialized. An array underflow is technically ok although the later CloseHandle() call will fail when it tries to free an essentially random handle. If you enter a larger number of threads you will overflow all your arrays (try it with num_of_thr=10 for example).
    • If it still doesn’t work try removing the threading to see if the threading or code itself is the source of the problem. For example, you can call the ThreadProc() function manually in a loop instead of from within threads. Either trace through the program with a debugger or output logs to stdout/file (which would also work in the threading model).
    • Instead of a random source matrix I would use a few fixed values at first with a known result. This will make it easier to determine if the code is actually computing the correct result.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

do you have idea, if there would be some nice way to browse/log JMS
I have this idea for a free backup application. The largest problem I need
I have an idea for how to solve this problem, but I wanted to
Somebody have idea how to use all cores for calculating integration? I need to
anyone have idea how to solve this problem counts the number of occurrences of
Anyone have idea about how to bind Boolean in sqlite with objective - c.
I have idea to write errors from my application to the Windows Event Viewer
Anyone have idea how to pass value from iPhone application to the REST-Webservice using
Any body have idea how to put controls dynamically in asp.net 3.5 ? if
I have an idea for a simple application which will monitor a group of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.