I executed the following code on Ubuntu 14.04 and CentOS 7 using gcc compiler but the strange thing is that it shows different output for same inputs. There are two issues that I'm unable to solve.
I always get 0 in sum(in main function) for the very first multiplication (1*1).
Completely unexpected output for Ubuntu.
Here is the code and both the outputs.
Code
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#define N 15
struct matrix
{
int num1, num2;
};
void* multiply(void *c);
int main()
{
int i, j, rows, cols, a[N][N], b[N][N], sum, k, final, res[N][N],*ptr;
pthread_t t1, t2;
struct matrix m1;
ptr=∑
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of cols: ");
scanf("%d", &cols);
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("Enter the value at: a[%d][%d] : ", i, j);
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("Enter the value at: b[%d][%d] : ", i, j);
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
final = 0;
for(k = 0; k < rows; k++)
{
m1.num1 = a[i][k];
m1.num2 = b[k][j];
pthread_create(&t1, NULL, (void*)multiply,(void*)&m1);
pthread_join(t1, (void**)&ptr);
sum=*ptr;
printf("\t%d",sum);
final += sum;
res[i][j] = final;
}
printf("\n");
}
}
printf("The result is :\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("%d\t", res[i][j]);
}
printf("\n");
}
return 0;
}
void* multiply(void *c)
{
struct matrix *m;
m = (struct matrix *)c;
int p = 0;
p = m->num1 * m->num2;
printf("\t%d * %d = %d",m->num1,m->num2,p);
pthread_exit((void*)&p);
}
Output for execution on Ubuntu
Enter the number of rows: 2
Enter the number of cols: 2
Enter the value at: a[0][0] : 1
Enter the value at: a[0][1] : 2
Enter the value at: a[1][0] : 3
Enter the value at: a[1][1] : 4
Enter the value at: b[0][0] : 1
Enter the value at: b[0][1] : 2
Enter the value at: b[1][0] : 3
Enter the value at: b[1][1] : 4
1 * 1 = 1 0 2 * 3 = 6 32648
1 * 2 = 2 32648 2 * 4 = 8 32648
3 * 1 = 3 32648 4 * 3 = 12 32648
3 * 2 = 6 32648 4 * 4 = 16 32648
The result is :
32648 65296
65296 65296
Output for execution on CentOS
Enter the number of rows: 2
Enter the number of cols: 2
Enter the value at: a[0][0] : 1
Enter the value at: a[0][1] : 2
Enter the value at: a[1][0] : 3
Enter the value at: a[1][1] : 4
Enter the value at: b[0][0] : 1
Enter the value at: b[0][1] : 2
Enter the value at: b[1][0] : 3
Enter the value at: b[1][1] : 4
1 * 1 = 1 0 2 * 3 = 6 6
1 * 2 = 2 2 2 * 4 = 8 8
3 * 1 = 3 2 4 * 3 = 12 4
3 * 2 = 6 3 4 * 4 = 16 16
The result is :
6 10
15 22
The multiply function is returning a pointer to a local variable p, and the lifetime of the local variable finishes as soon as the function ends.
The easiest solution here is not to use the return value, but to reserve a place for the result in the struct matrix that is passed to multiply(), because that structure is allocated within main. Change the definition of struct multiply:
struct matrix
{
int num1, num2;
int product;
};
Change multiply() to put the result here:
void *multiply(void *c)
{
struct matrix *m = c;
m->product = m->num1 * m->num2;
printf("\t%d * %d = %d", m->num1, m->num2, m->product);
return NULL;
}
Change main() to retrieve the result from there:
pthread_create(&t1, NULL, multiply, &m1);
pthread_join(t1, NULL);
sum = m1.product;
(Side note: that variable sum has a confusing name, since it doesn't hold a sum!)
Related
I want to find the sum of all the positive integers in the range [1, N] with a given digit sum d. For example, if n = 100 and d = 7, the answer will be 7 + 16 + 25 + 34 + 43 + 52 + 61 + 70 = 308.
Following code can be used to count the numbers in the range [1, N] with a given digit sum d.
cnt[i][0][s] denotes count of suffixes that can be formed starting from index i, whose digits add up to s.
cnt[i][1][s] count of suffixes that can be formed starting from index i, whose digits add up to s such that the formed suffix is not greater than corresponding suffix in input string
#include <bits/stdc++.h>
using namespace std;
typedef long long int i64;
i64 cnt[20][2][200];
void digit_sum_dp(string ss) {
int n = ss.size();
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 200; k++) {
cnt[i][j][k] = 0;
}
}
}
cnt[n][0][0] = 1;
cnt[n][1][0] = 1;
for (int i = n - 1; i >= 0; i--) {
for (int tight = 0; tight < 2; tight++) {
for (int sum = 0; sum < 200; sum++) {
if (tight) {
for (int d = 0; d <= ss[i] - '0'; d++) {
if (d == ss[i] - '0') {
cnt[i][1][sum] += cnt[i + 1][1][sum - d];
} else {
cnt[i][1][sum] += cnt[i + 1][0][sum - d];
}
}
} else {
for (int d = 0; d < 10; d++) {
cnt[i][0][sum] += cnt[i + 1][0][sum - d];
}
}
}
}
}
return cnt[0][1][d];
}
int main() {
string str = "100";
int d = 7;
cout << digit_sum_dp(str, d) << "\n";
return 0;
}
I have tried to extend the code to find out the sum of numbers instead of the count of numbers. Following is a code snippet.
cnt[i][1][sum] += cnt[i + 1][1][sum - d];
tot[i][1][sum] += (d * cnt[i + 1][1][sum - d] + tot[i + 1][1][sum - d] * pow(10, i));
I am getting incorrect results for some of the inputs. I shall be grateful if someone can help me.
I have a function with 3 variables that can be modified based on random chance. For example a random number is picked from a range of 100, if random number is in range of 0 to 9, A is modified (10%), B is 11-30 (20%), C is 31 to 35 (5%).
I have rewritten this function so that the 3 variables all have a chance to be modified in the same call (random number is generated 3 times). What number ranges would I give to each variable if I wanted the overall chance of each variable being selected to be the same as the previous behaviour?
I decided to write a quick Java app to answer my question:
import java.util.*;
import java.lang.*;
class Main {
public static void main(String args[])
{
int a = 0;
int b = 0;
int c = 0;
System.out.println("old: ");
for (int i=0; i<10000; i++) {
int rand = random();
if (rand < 10) {
a++;
continue;
}
if (rand >= 10 && rand < 20) {
b++;
continue;
}
if (rand >= 20 && rand < 30) {
c++;
continue;
}
}
System.out.println("a= " + a);
System.out.println("b= " + b);
System.out.println("c= " + c);
int total = a+b+c;
System.out.println("total " + total);
a = 0;
b = 0;
c = 0;
System.out.println("new: ");
for (int i=0; i<10000; i++) {
int rand = random();
if (rand < 10) {
a++;
}
rand = random();
if (rand >= 10 && rand < 20) {
b++;
}
rand = random();
if (rand >= 20 && rand < 30) {
c++;
}
}
System.out.println("a= " + a);
System.out.println("b= " + b);
System.out.println("c= " + c);
total = a+b+c;
System.out.println("total " + total);
}
public static int random() {
return (int)(Math.random() * 100);
}
}
and the results between "new" and "old" were very similar:
old:
a= 1000
b= 1005
c= 1001
total 3006
new:
a= 949
b= 1023
c= 999
total 2971
So as it turns out, no difference at all.
I am attempting to make a multithreaded program that computes the prime-factor of each input(each input having its own thread). Using realloc or calloc with a int* Name, inputing large numbers or lots of thread {1..100} gives me realloc:invalid size or malloc.c failure. I am not sure if it is a deep pointer issue, but I need an output of such:
./thread {1..10}
1:
2: 2
3: 3
4: 2 2
5: 5
etc....
10: 2 5
int size = 5;
int* primeNumbers = (int*)calloc(size,sizeof(int));
int n = atoi(param);
//sum = 0;
int counter = 0;
//Add original value to int* primeNumbers for return printing
primeNumbers[counter] = n;
counter++;
while (n % 2 == 0)
{
printf("%d ", 2);
//If max allocated memory is reached, double memory size
if (counter == size)
{
size = size * 2;
primeNumbers = realloc(primeNumbers,size*sizeof(int));
primeNumbers[size] = 0;
}
primeNumbers[counter] = 2;
counter++;
n = n/2;
}
for (int i = 3; i <= sqrt(n); i = i + 2)
{
while (n % i == 0)
{
printf("%d ",i);
//If max allocated memory is reached, double memory size
if (counter == size)
{
size = size * 2;
primeNumbers = realloc(primeNumbers,size*sizeof(int));
primeNumbers[size] = 0;
}
primeNumbers[counter] = i;
counter++;
n = n/i;
}
}
enter code here
I am getting this kind of weird error, I wrote a function to find the minimum sub array sum. But this doesn't work when the values of array start from 1 to size for value 1 2 3 4. I get timeout, but same code for 1 2 3 4 5 gives the correct answer. If I put a statement print statement above min_sub_array it gives the right answer for all values. It also works fine when the values start from 0 to size - 1
int min_sub_array_sum(int d[], int size)
{
for(int i = 1; i <= size; i++)
d[i] = -d[i];
int max_end_i = d[1], max_so_far = d[1];
for(int i = 2; i <= size; i++)
{
max_end_i += d[i];
if(max_so_far < max_end_i)
max_so_far = max_end_i;
if(max_end_i < 0)
max_end_i = 0;
}
return (-max_so_far);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int d[n];
for(int i = 1; i <= n; i++)
cin>>d[i];
cout<<min_sub_array_sum(d, n)<<'\n';
}
}
I should get the value of 1 for 1 2 3 4. But I am not getting.
You are accessing the nth index of array d[n] which does not exist.
This program checks distinctness in an array. (no repeated values in an array I.e if 1 2 3 3 4 is an array it is not distinct). this code Won't compile although (I believe that) index of array did not go out of range in for loop.
theRun-Time Check Failure says stack around variable 'n' was corrupted when I enter n =12. BUT says stack around variable 'A' was corrupted when I enter n = 10. with exactly the same variables entered in the array in the second step. (the error shows up after entering the fourth integer)
#include <iostream>
using namespace std;
int main()
{
int n;
int A[] = {0};
int integer;
cout<<"Enter the size of the array\n";
cin>>n;
cout<<"enter "<<n<<" integers\n";
for (int i = 0 ; i < n ; i++)
{
cin>>A[i];
}
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n - i; j++)
{
if(A[j+1] > A[j])
{
int temp;
temp = A[j];
A[j+1] = A[j];
A[j+1] = temp;
}
}
}
for (int i = 0 ; i < n; i++)
{
if (A[i] - A[i+1] ==0 ){
cout<<"\nThe Array Is Not Distinct !\n";
break;
}
else
{
cout<<"\nThe Array Is Distinct !\n";
}
}
system("pause");
return 0;
}