OpenMp only one thread executes [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have set 4 threads.Screen1 But only one threads executes in parallel region (look at "id"). Screen2.
Available threads (I used omp_get_max_threads()).
What is wrong?
int main(int argc, char** argv)
{
srand(time(NULL));
int P = atoi(argv[1]);
omp_set_num_threads(4);
#pragma parallel for
for( int i = 0; i < 1000000; i++ ) {
printf("%d\n",omp_get_max_threads());
printf("Num Threads:%d ",omp_get_num_threads());
printf("id:%d\n",omp_get_thread_num());
}
while(1);
const uint num_elements = 79;
int* data = (int*) malloc(sizeof(int) * num_elements);
for( uint i = 0; i < num_elements; i++ ) {
data[i] = rand() % 100 - 50;
}
int* buffer = (int*) malloc(sizeof(int) * num_elements);
PrintArray(data,0,num_elements);
int* res = MergeSort(data,buffer,0,num_elements - 1,8);
PrintArray(res,0,num_elements);
return 0;
}

The error was: #pragma parallel for.
Correct instruction is "#pragma omp parallel for"

printf("%d\n",omp_get_max_threads());
printf("Num Threads:%d ",omp_get_num_threads());
printf("id:%d\n",omp_get_thread_num());
Please place these three printf into one printf. Easy. Then remove {} the brackets from the for loop,
and it works, I tested. It's just that I am writing here from my smartphone. I am going to add to github the basic for sample later.

Related

Heap block error while freeing a pointer array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 19 days ago.
Improve this question
I wrote this program that basically allocate a matrix of chars using pointer.
Here's the code:
char **bitmap;
void create_bitmap() {
int i;
int columns = (int) ceil(m / 8) + 1;
bitmap = (char **) malloc(sizeof(char) * n);
for (i = 0; i < n; i++)
bitmap[i] = (char *) calloc(columns, sizeof(char));
}
void free_bitmap() {
int i;
for (i = 0; i < n; i++) {
free(bitmap[i]);
}
free(bitmap);
}
The heap block error comes out when at the last instruction of the free_bitmap() procedure.
I just can't figure out what triggers the error.
Thanks in advance.
The error was simple and I guess this is the final proof that I am not focused this morning.
In the procedure create_bitmap(), the malloc istruction would have been written:
bitmap = (char **) malloc(sizeof(char *) * n);
Now, this istruction will allocate n char* pointers, instead of only a char.
Pardon.

C simple question of getting average value from array each components addition [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is a c code for getting the average value of the addition of array components.
But once I run this which is not outputting anything.
Can anyone help me out where I got the code wrong?
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
double solution(int arr[], size_t arr_len);
int main()
{
int array[10] = { 1,2,3,4,5,6,7,8,9,10 };
int length = sizeof(array[10]);
double out = solution(array, length);
printf("solution is %f\n", out);
return 0;
}
double solution(int arr[], size_t arr_len) {
double answer = 0;
int total = 0;
for (int i = 0; i < arr_len;){
total += arr[i];
}
answer = total / arr_len;
return answer;
}
You are not incrementing the loop counter in solution so its stuck in an infinite loop.
for (int i = 0; i < arr_len;){
needs to be
for (int i = 0; i < arr_len; i++) {
Edit:
sizeof is also wrong. It returns the total memory used by the array. So you need to do
int length = sizeof(array) / sizeof(array[0])
which divides the total memory by the size of one element to give you the total number of elements.

Define and initialize an array of strings in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im trying to define and initialize an array of strings using a function, but, the function is causing segmentation fault while allocating memory for each of the pointers.
Please find below the minimal reproducible version of the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COL 100
#define ROW 10
int init_arr(char ***arr_ptr) {
*arr_ptr = malloc(sizeof(char *) * ROW);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
*arr_ptr[temp_iter] = malloc(COL + 1);
strncpy(*arr_ptr[temp_iter], "MY_STRING_IS_THIS", COL);
}
return 0;
}
int main() {
char **arr_of_str = NULL;
init_arr(&arr_of_str);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
printf("\nData: %s", arr_of_str[temp_iter]);
}
}
This code works fine when the function is split into 2 where the first function defines it and 2nd one initializes it.
The problem lies here *arr_ptr[temp_iter], first dereference(in his case a subscription) that was applied was from box brackets, and then from asterix. What you want to do is separate these dereferences, so that asterix is applied first and the from box brackets, like this:
int init_arr(char*** arr_ptr) {
*arr_ptr = (char**) malloc(sizeof(char *) * ROW);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
(*arr_ptr)[temp_iter] = (char*) malloc(COL + 1);
strncpy((*arr_ptr)[temp_iter], "MY_STRING_IS_THIS", COL);
}
return 0;
}
Also, you need to cast the pointer from malloc into appropriate one, since the malloc always returns void*, and you are dereferencing void pointer later on.

rand() doesn't give me random numbers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to print 1000 random numbers saved in a array. Numbers have to be between 1 and 10000.
I put srand(time(NULL)) in my main function and the array have to be filled with random numbers in my init function. The ausgabe function is for formatted output.
But rand fills my array with numbers all in row.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 1000
void init(int * ptr, int size){
for (int i = 0; i < size; i++){
*(ptr + i) = (rand() %10000+1);
}
}
void ausgabe(int * ptr, int size){
for (int i = 0; i < size; i++){
printf("%5i\t", * ptr + i);
if ((i + 1) %10 == 0){
printf("\n");
}
}
printf("\n\n");
}
int main(){
int zufall[ARR_SIZE];
srand(time(NULL));
init(zufall, ARR_SIZE);
printf("\n\t\t\t---unsortierte Ausgabe---\n\n");
ausgabe(zufall, ARR_SIZE);
return 0;
}
* ptr + i is (*ptr)+i, not *(ptr+i). You need to be more careful with operator precedence. (And to learn to use your debugger: 30 seconds in your debugger would have clearly revealed that the problem was the printing, not the initialization.)

Sorting an array - C language [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have tried to write a sorting function sort(int *buffer, int array[], int size) which works in a way similar to the insertion sort - it takes the first element from the array, sets it as the first element of the buffer and then checks whether or not the next value showing up in the array is greater than the last value stored in the buffer. If yes, it keeps swapping the two elements until everything is in its place. This is my minimal working example:
#include <stdio.h>
void sort(int *buffer, int array[], int size) {
for(int i = 0; i < size; i++) {
buffer[i] = array[i];
while(i >= 1 && buffer[i] < buffer[i-1]) {
int tmp = buffer[i-1];
buffer[i-1] = buffer[i];
buffer[i] = tmp;
printf("i = %d i: %d, i -1 : %d \n",i, buffer[i], buffer[i-1]);
i--;
}
}
}
int main(void) {
int array[3] = {4,3,2};
int buffer[3];
sort(buffer, array, 3);
for(int i = 0; i < 3; i++) {
printf("%d", buffer[i]);
}
}
However, the output of this program is 222
To be honest, I don't see how it's even possible that three identical elements got placed in the buffer.
What can have gone wrong?
You are using the same variable for the inner while cycle and for the outer for loop. Use a different variable and copy the value of i to it in each iteration of the for.

Resources