I have to pass two arrays ( A and B with dimensions of 5X4) to two functions called FUNCTION_1 and FUNCTION_2. Both of arrays columns and rows should be passed as POINTERS. FUNCTION_1 will take each element of A array and calculate the sum of prime factors of each element located in A( (with the help of another function called sumPrime), then it will store these sums in array B. FUNCTION_2 has to print both of A and B arrays. ( The normal numbers array, and the prime factors sums array). There are some additions in the program which are not important now but I am going to show them too in respect of clearness.
#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 4
# include <math.h>
int sumPrime(int number){
int factor = 2;
int sum=0;
while(1!=number){
if(number%factor==0){
number /= factor;
sum+=factor;
factor = 2;
continue;
}
factor++;
}
return sum;
}
int FUNCTION_1(int *a[][20],int *b[][20],int row, int col){
int c,d;
for(c=0;c<row;c++){
for(d=0;d<col;d++){
b[c][d]=sumPrime(a[c][d]);
return b[c][d];
}
}
}
void FUNCTION_2(int *x[][20],int *y[][20],int rows, int cols){
printf(" \n A matrix is :\n");
int e,f;
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,x[e][f]);
}
}
printf("\n B matrix is:\n");
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,FUNCTION_1(x,rows,cols,y,rows,cols));
}
}
}
int main(){
int A[ROW][COL]={0};
int B[ROW][COL]={0};
int x=1;
int i;
int j;
for(i=0;i<ROW;i++){
for(j=0;j<COL;j++){
A[i][j]=x;
x=x+2;
}
}
printf("%d",A[0][0]);
return 0;
}
When trying to perform FUNCTION_1 or FUNCTION_2 I get many errors or even when defining the functions. There is no problem with defining A matrix or prime function! HELP!!
Your definition for a matrix is wrong ..
Use :
func(int* a[20] , int cols)
in this case you will have 20 rows ,
or
func(int a[][20] , int rows)
And define some macro for the columns if it is fixed value .
Related
I wanted to know how to get the result like: Number x is prime or Number x is not prime, sure this threaded ex. in 10 parts. How to pass argument 3 if we have an array and how the function checks an array if is prime or not? How can I do that? Thanks in advance...I'm a beginner so sorry for any mistakes that I did in the code
Code:
#include <stdio.h>
#include ...
//defining global variables
#define ARRAYSIZE 1000000
#define MAXTHREAD 10
int arr[ARRAYSIZE];
//since we split array in parts,we need a variable to reprensent a part of array
int part=0;
//Function to fill the array with values
void fill_array(int arr[],int size){...}
//Function to check if a numer is prime or not
int prime(long int num){
//return 0:not prime
//return 1:prime
//Function to deside,if a element of the array is prime or not(threaded)
void* prime_threaded(void *vargp){
//casting a void pointer to integer
int num=*((int *)vargp);
int thread_part=part++;
int start=thread_part*(ARRAYSIZE/MAXTHREAD);
int end=(thread_part +1)*(ARRAYSIZE/MAXTHREAD);
printf("Thread works in segment %d\n",part);
int* returnvalue = (int*) malloc(sizeof(int));
for(int i=start;i<end;i++){
if(prime(num==0)){
*returnvalue=0;
return returnvalue;
}}
*returnvalue=1;
return returnvalue;}
int main(){
fill_array(arr,ARRAYSIZE);
pthread_t threads[MAXTHREAD];
for(int i=0;i<MAXTHREAD;i++){
pthread_create(&threads[i],NULL,prime_threaded,(void*)arr);
}
for(int i = 0; i < MAXTHREAD; i++) {
void * exit_status_thread;
pthread_join(threads[i], &exit_status_thread);
int result = *(int *) exit_status_thread;
free(exit_status_thread);
if(result==0){
printf("\n the number is not prime\n ");
}else{
printf("\n the number is prime\n");
}
}
return 0;
}
i'm new to c. i'm writing a .h file dedicated to matrix. I want to write a function in .h file that returns a matrix (array) (not possible in c), so the real return is a pointer to a local array variable. But i can't use a local pointer in the main funct, so i've changed the int matrix[][] to static int matrix[][]. The problem now is: the user insert the number N of rows/columns, but a static array can only take a constant dimension. help
This is the .h
int N;
int i;
int j;
int *get_matrix(){
int user_input;
printf("set the dimension NxN of your matrix >> N=");
scanf("%d",&N );
static int temp_matrix[N][N];
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("insert the matrix[%d][%d] value\n",i,j );
scanf("%d",&user_input);
temp_matrix[i][j]=user_input;
}
}
return temp_matrix;
}
void print_matrix(int *matrix){
for(i=0;i<sizeof(matrix)/4;i++){
for(j=0;j<sizeof(matrix)/4;j++){
printf("%7d",matrix[i][j]);
printf("\n");
}
}
}
this is the main.c file
#include <stdio.h>
#include "matrix_math.h"
void main(void){
int i;
int j;
int *p1 = get_matrix();
int matrix1[N][N];
for(i=0;i<N;i++){
for(j=0;j<N;j++){
matrix1[i][j]=p[i][j];
}
}
print_matrix(matrix1);
}
If this is just a school homework, and malloc not allowed/needed, and assuming it is acceptable to limit code to some reasonable upper limit N, consider defining matrix as a struct
#define MAXN 20
struct matrix {
int n ;
int data[MAXN][MAXN] ;
}
// matrix.c
struct matrix get_matrix() { ... ; return m } ;
void print_matrix(struct m *mp) {
for (int i = 0 ; i<mp->n ; i++) {
...
} ;
} ;
And then you can pass "matrix" around. Needless to say, better to pass matrix * whenever possible to improve performance. You can also make functions that return matrix, if needed.
My real goal here is to initialize a 2d array,
then call a function to get user input for the rows and columns, and then return the 2d array to main with the size that the user specifies.
Though I understand that it would be far more efficient to not use the GetData function to ask for user input, and instead put the input into the main function, my professor requires that I do.
This is the specific Prompt:
"GetData(): write a function that takes the reference of a 2D array, reference of variables, one for number of rows and another for number of columns as parameter.
In the function, it takes number of rows and number columns as user input and update the variable passed to it.
Then it takes the input for the matrix based on number of rows and columns."
#include <stdio.h>
void GetData(int *rows, int *columns, int matrix[][*columns]);
void Display(int rows, int columns, int matrix[][columns]);
int main(void)
{
int r1, c1, r2, c2;
int m1[r1][c1];
int m2[r2][c2];
printf("Working for Matrix 1\n");
GetData(&r1,&c1,m1);
Display(r1,c1,m1);
return 0;
}
void GetData(int *rows, int *columns, int matrix[][*columns])
{
// maybe instead of void it needs to return an integer pointer?
int a, b, i, j,k;
printf("Enter number of rows and columns: ");
scanf("%d %d",&a,&b);
*rows = a;
*columns = b;
printf("Enter data for %dx%d matrix",*rows,*columns);
for(i = 0;i<*rows;i++)
{
printf("\nrow %d\n",i+1);
for(j=0;j<*columns;j++)
{
scanf("%d",&k);
matrix[i][j] = k;
}
}
}
void Display(int rows, int columns, int matrix[][columns])
{
int i, j;
for (i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
printf("%4d",matrix[i][j]);
}
}
When this code is run, there is also a segmentation error, I am not too sure as of what that means or what is causing it.
When you declare: int m1[r1][c1], r1 and c1 are unitialized. Thus, the compiler has no idea how large to make m1. Likewise for m2.
If you compiled with (e.g.) -O2 -Wall, the compiler would flag this for you.
You need something like:
int
main(void)
{
int r1 = 10;
int c1 = 12;
int m1[r1][c1];
int r2 = 5;
int c2 = 7;
int m2[r2][c2];
...
return 0;
}
However, you prompt the user for the array dimensions in GetData, but that's too late in the process. I'd move that prompt into main:
int
main(void)
{
int nrow;
int ncol;
printf("Enter number of rows and columns: ");
scanf("%d %d", &nrow, &ncol);
int m1[nrow][ncol];
int m2[nrow][ncol];
...
return 0;
}
I have an array having 100 numbers say a[1,2,3,4,5,6....98,99,100]. I want to divide it into 25 groups with each group containing 4 elements and then subtract elements of other blocks with each element of one block. For example: if three blocks out of 25 are labelled as A,B,C and contain elements as:
A [1,2,3,4],
B[5,6,7,8] &
C[9,10,11,12]
then subtraction is to be done like this:
(A-B, A-C),
(B-A, B-C) &
(C-A, C-B)
i.e.
1-5,1-6,1-7,1-8,1-9,1-10,1-11,1-12; then
2-5,2-6,2-6,2-8,2-9,2-10,2-11,2-12; then
3-5,3-6,3-7,3-8,3-9,3-10,3-11,3-12; then
4-5,4-6,4-7,4-8,4-9,4-10,4-11,4-12;
THEN
5-1,5-2,5-3,5-4,5-9,5-10,5-11,5-12;
and like wise..
Can anyone help me in writing C program for this.
The code that I have written is partial and not doing the above task completely. Code is:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100]={1,2,3,4.....,98,99,100};
int i=0, j=0;
int x[100], y[100];
// considering only 12 numbers for the sake of simplicity
for (i=0;i<12;i++)
{
for(j=0;j<8;j++)
{
x[j] = a[i] - a[r+4];
}
y[i] = x[i];
}
}
#include <stdio.h>
int main(void) {
int N, i;
scanf("%d", &N);
int numArray[N]; // Define an array of four integers
// Get inputs for the array elements
for (i=0;i<N; i++) {
scanf("%d", &numArray[i]);
}
int sum = 0;
// Write here the logic to add these integers:
for (i=0;i<N;i++) sum += numArray[i];
printf("%d\n",sum); // Print the sum
return 0;
}
I've got a function thats able to produces 2 arrays, one when the index 'i' is even and one when the index 'i' is odd, so I end up with two arrays. The even 'i' array is called W_e and made of N elements, the odd 'i' array is called W_o and also made of N elements.
I now need to be merge these two arrays into another array Wn (which has 2*N elements) such that it looks like Wn=[W_e[0],W_o[0],W_e[1],W_o[1],...,W_e[N-1],W_o[N-1]] but I'm not sure how to do it. I tried to use nested loops but it didn't work. The arrays W_e and W_o are produced correctly according to my calculations, I'm just unable to combine the entries into one array.
This is what I have so far. I have not done anything in the main function except call the function which is giving me trouble "double *MakeWpowers(int N);". Please keep in mind this works for N>2, I have not yet dealt with N=1 or N=2.
Any help will be greatly appreciated. Thank you!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#define pi 4*atan(1)
double *MakeWpowers(int N);
void print_vector(double *x,int N);
double *make_vector(double *x,int N);
int main(void)
{ int N;
double *Wn;
printf("\n Please enter the size of the NxN matrix:\n");
scanf("%d",&N);
Wn=MakeWpowers(N);
//print_vector(Wn,N);
free(Wn);
return(0);
}
double *MakeWpowers(int N)
{
double *Wn,*W_e,*W_o;
int i,j;
Wn=make_vector(Wn, 2*N);
W_e=make_vector(W_e, N);
W_o=make_vector(W_o, N);
for(i=0;i<=N-1;i++)
{
if(i%2==0)
{
W_e[i]=cos((2*i*pi)/N);
}
else
{
W_o[i]=sin((2*i*pi)/N);
}
}
printf("\nThis is the even vector W_e:\n");
print_vector(W_e, N);
printf("\nThis is the odd vector W_o:\n");
print_vector(W_o, N);
for(j=0;j<2*N;j++)
{
if(j%2==0)
{Wn[j]=W_e[i];}
//++i;}
else
{Wn[j]=W_o[i];}
//++i;}
printf("\nthis is Wn:\n\n");
print_vector(Wn, 2*N);
//Wn[j]=W_o[i];
//j++;
}
return(Wn);
}
void print_vector(double *x,int N)
{
int i;
for (i=0; i<N; i++)
{
printf ("%9.4f \n", x[i]);
}
printf("\n");
}
double *make_vector(double *x,int N)
{ int i;
double xi;
x=(double *)malloc((N)*sizeof(double));
for(i=0;i<N;i++)
{
x[i]=(double)xi;
}
return(x);
}
Here's the general logic to it:
LET a, b, merge BE ARRAYS
FOR k IN 0..length(a)
merge[2*k] = a[i]
merge[2*k+1] = b[i]
RETURN merge
a makes the even entries (2k), b the odd ones (2k+1).
This is probably wrong
double *make_vector(double *x,int N)
{ int i;
double xi;
You have to initialize variable xi same thing goes for *Wn,*W_e,*W_o