can You help me split these files on:
header.h (this was already made by me)
dane.cpp (this was already made by me)
solve.cpp (all solve patterns, inverse matrix, x1, x2, and f)
main.cpp
I did first two, but I cannot manage other ones.
header:
void dane(float(*q)[2], float *p);
data:
#include "Header.h"
void dane(float(*q)[2], float *p)
{
printf("Write 4 numbers to Matrix: A\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j <2; j++)
{
scanf_s(" %f", &q[i][j]);
}
}
printf("\nWrite vector: B\n");
for (int i = 0; i < 2; i++)
scanf_s(" %f", &p[i]);
}
source:
#include "Header.h"
float A[2][2], B[2], X[2];
void main(void)
{
float AA[2][2];
float h, det, value;
data(A, B);
system("cls");
printf("Matrix A: \n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
printf("[%5.2f] ", A[i][j]);
printf("\n");
}
printf("\n\nVector: \n");
for (int i = 0; i < 2; i++)
printf("\n[%5.2f] ", B[i]);
_getch();
det = (A[0][0] * A[1][1]) - (A[0][1] * A[1][0]);
AA[0][0] = (1 / det) * A[1][1];
AA[0][1] = (1 / det) * (-1)*A[1][0];
AA[1][0] = (1 / det) * (-1)*A[0][1];
AA[1][1] = (1 / det) * A[0][0];
h = AA[1][0];
AA[1][0] = AA[0][1];
AA[0][1] = h;
if (det <= 0)
{
printf("\n\nDoesnt have extreme.");
_getch();
}
else
{
printf("\n\n\nInv matrix: \n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
printf("[%5.2f] ", AA[i][j]);
printf("\n");
}
_getch();
X[0] = 0.5 * AA[0][0] * (-1) * B[0] + 0.5 * AA[0][1] * (-1) * B[1];
X[1] = 0.5 * AA[1][0] * (-1) * B[0] + 0.5 * AA[1][1] * (-1) * B[1];
printf("\n x1 = %5.2f", X[0]);
printf("\n x2 = %5.2f", X[1]);
value = A[0][0] * X[0] * X[0] + A[1][0] * 2 * X[0] * X[1] +
A[1][1] * X[1] * X[1] + B[0] * X[0] + B[1] * X[1];
printf("\n\n f = %5.2f", value);
_getch();
}
}
Keep prototypes in header-files and surround them with #ifndef. Like the following,
Header.h
#ifndef HEADER_H
#define HEADER_H
void dane(float(*q)[2], float *p);
#endif
Related
I'm trying to write a function that does naive matrix multiplication of two contiguous, row-major arrays. But when I attempt to print each value at the end I get garbage. I'm guessing it's because I've mixed up the proper iterations and scaling needed to jump rows/columns. Does anyone have any advice?
Full code necessary is below:
#include <stdio.h>
#include <stdlib.h>
void dmatmul(double *a, double *b, double *c, int astride, int bstride, int cdim_0, int cdim_1) {
int i, j, p;
for (i = 0; i < cdim_0; i++) {
for (j = 0; j < cdim_1; j++) {
c[i * cdim_1 + j] = 0.0;
for (p = 0; p < (astride); p++) {
c[i * cdim_1 + j] += a[i * (astride) + p] * b[p * (bstride) + j];
}
}
}
}
int main(void) {
double *x, *y, *z;
int xdim_0, xdim_1, ydim_0, ydim_1, zdim_0, zdim_1, i, j;
xdim_0 = 2;
xdim_1 = 4;
ydim_0 = 4;
ydim_1 = 2;
zdim_0 = 2;
zdim_1 = 2;
x = (double *) malloc (xdim_0 * xdim_1 * sizeof(double));
y = (double *) malloc (ydim_0 * ydim_1 * sizeof(double));
z = (double *) malloc (zdim_0 * zdim_1 * sizeof(double));
for (i = 0; i < xdim_0 * xdim_1; i++) {
x[i] = i + 1;
y[i] = 2 * (i + 1);
}
dmatmul(x, y, z, xdim_1, ydim_1, zdim_0, zdim_1);
printf("\nMatrix product of X and Y dimensions: (%d, %d)\n", zdim_0, zdim_1);
printf("Matrix product of X and Y values:");
for (i = 0; i < zdim_0; i++) {
printf("\n");
for (j = 0; j < zdim_1; i++) {
printf("\t%f", z[i * zdim_1 + j]);
}
}
return 0;
}
The primary problem is a typo in the inner for loop doing the printing. You have:
for (j = 0; j < zdim_1; i++)
but you ned to increment j, not i:
for (j = 0; j < zdim_1; j++)
Here's my code, which has an independent matrix printing function appropriate for the arrays you're using:
/* SO 7516-7451 */
#include <stdio.h>
#include <stdlib.h>
static void dmatmul(double *a, double *b, double *c, int astride, int bstride, int cdim_0, int cdim_1)
{
int i, j, p;
for (i = 0; i < cdim_0; i++)
{
for (j = 0; j < cdim_1; j++)
{
c[i * cdim_1 + j] = 0.0;
for (p = 0; p < (astride); p++)
{
c[i * cdim_1 + j] += a[i * (astride) + p] * b[p * (bstride) + j];
}
}
}
}
static void mat_print(const char *tag, int rows, int cols, double *matrix)
{
printf("%s (%dx%d):\n", tag, rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("%4.0f", matrix[i * cols + j]);
putchar('\n');
}
}
int main(void)
{
int xdim_0 = 2;
int xdim_1 = 4;
int ydim_0 = 4;
int ydim_1 = 2;
int zdim_0 = 2;
int zdim_1 = 2;
double *x = (double *)malloc(xdim_0 * xdim_1 * sizeof(double));
double *y = (double *)malloc(ydim_0 * ydim_1 * sizeof(double));
double *z = (double *)malloc(zdim_0 * zdim_1 * sizeof(double));
for (int i = 0; i < xdim_0 * xdim_1; i++)
{
x[i] = i + 1;
y[i] = 2 * (i + 1);
}
mat_print("X", xdim_0, xdim_1, x);
mat_print("Y", ydim_0, ydim_1, y);
dmatmul(x, y, z, xdim_1, ydim_1, zdim_0, zdim_1);
mat_print("Z", zdim_0, zdim_1, z);
printf("\nMatrix product of X and Y dimensions: (%d, %d)\n", zdim_0, zdim_1);
printf("Matrix product of X and Y values:\n");
for (int i = 0; i < zdim_0; i++)
{
for (int j = 0; j < zdim_1; j++)
printf("\t%f", z[i * zdim_1 + j]);
printf("\n");
}
return 0;
}
I've also initialized the variables as I declared them. The code should, but does not, check that the memory was allocated.
When I ran this code without your printing, I got the correct result, so then I took a good look at that and saw the problem.
X (2x4):
1 2 3 4
5 6 7 8
Y (4x2):
2 4
6 8
10 12
14 16
Z (2x2):
100 120
228 280
Matrix product of X and Y dimensions: (2, 2)
Matrix product of X and Y values:
100.000000 120.000000
228.000000 280.000000
Download or See Image Here
The problem statement is that I need to perform dilation and erosion on a binary image and using a structure element of dimension given by the user typically a 1D or 2D array and it can be square matrix(n X n) or rectangle matrix (m X n) where m, n can be even or odd integers.
This is morphological image processing using C language.
I implemented the following code, but I am not getting output as expected. Can any one tell me my mistake?
I am taking origin as a // 2 and b // 2 of structuring element which is order of a X b.
The following is my main function in image processing.
#include "DEOC.h"
int main(int argc, char *argv[])<br>
{
FILE *fp, *op1, *op2, *op3, *op4;
fp = fopen(argv[1], "r");
op1 = fopen(argv[2], "wb");
op2 = fopen(argv[3], "wb");
op3 = fopen(argv[4], "wb");
op4 = fopen(argv[5], "wb");
if (fp == NULL)
{
printf("There is a problem in file opening!!! Please check again!!!");
exit(0);
}
int i, j, col, row, x, y, temp;
char ch1, ch2;
fscanf(fp, "%c%c", &ch1, &ch2);
fscanf(fp, "%d %d", &col, &row);
fprintf(op1, "%c%c\n", ch1, ch2);
fprintf(op1, "%d %d\n", col, row);
fprintf(op2, "%c%c\n", ch1, ch2);
fprintf(op2, "%d %d\n", col, row);
fprintf(op3, "%c%c\n", ch1, ch2);
fprintf(op3, "%d %d\n", col, row);
fprintf(op4, "%c%c\n", ch1, ch2);
fprintf(op4, "%d %d\n", col, row);
int *img, *dialated_img, *eroted_img, *opened, *closed, *structure;
img = (int *)malloc(row * col * sizeof(int));
dialated_img = (int *)malloc(row * col * sizeof(int));
eroted_img = (int *)malloc(row * col * sizeof(int));
opened = (int *)malloc(row * col * sizeof(int));
closed = (int *)malloc(row * col * sizeof(int));
printf("Enter x: - ");
scanf("%d", &x);
printf("Enter y: - ");
scanf("%d", &y);
structure = (int *)malloc(x * y * sizeof(int));
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
*(structure + i * y + j) = 1;
}
}
printf("Used Structure is: -\n");
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
printf("%d ", *(structure + i * y + j));
}
printf("\n");
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
fscanf(fp, "%d", &temp);
*(img + i * col + j) = temp;
*(dialated_img + i * col + j) = temp;
*(eroted_img + i * col + j) = temp;
*(opened + i * col + j) = temp;
*(closed + i * col + j) = temp;
}
}
dialation(img, dialated_img, structure, row, col, x, y);
erosion(img, eroted_img, structure, row, col, x, y);
opening(img, opened, structure, row, col, x, y);
closing(img, closed, structure, row, col, x, y);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
fprintf(op1, "%d", *(dialated_img + i * col + j));
fprintf(op2, "%d", *(eroted_img + i * col + j));
fprintf(op3, "%d", *(opened + i * col + j));
fprintf(op4, "%d", *(closed + i * col + j));
}
fprintf(op1, "\n");
fprintf(op2, "\n");
fprintf(op3, "\n");
fprintf(op4, "\n");
}
fclose(fp);
fclose(op1);
fclose(op2);
fclose(op3);
fclose(op4);
return 0;
}
The DEOC.h file is
#include <stdio.h>
#include <stdlib.h>
void dialation(int *img, int *dialation_img, int *structure, int row, int col, int x, int y)
{
int s = x / 2;
int a = y / 2;
for (int i = s; i < row - s; i++)
{
for (int j = a; j < col - a; j++)
{
if (*(img + i * col + j) == 1)
{
continue;
}
else
{
for (int w = -1 * s; w <= s; w++)
{
for (int q = -1 * a; q <= a; q++)
{
if (*(structure + (w + s) * y + (q + a)) == 1)
{
if (*(img + (i + w) * col + (j + q)) == 1)
{
*(dialation_img + i * col + j) = 2;
continue;
}
}
}
}
}
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (*(dialation_img + i * col + j) == 2)
{
*(dialation_img + i * col + j) = 1;
}
}
}
}
void erosion(int *img, int *erosion_img, int *structure, int row, int col, int x, int y)
{
int s = x / 2;
int a = y / 2;
for (int i = s; i < row - s; i++)
{
for (int j = a; j < col - a; j++)
{
for (int w = -1 * s; w <= s; x++)
{
for (int q = -1 * a; q <= a; y++)
{
if (x == 0 && y == 0)
{
continue;
}
else
{
if (*(structure + (w + s) * y + (q + a)) == 1)
{
if (*(img + (i + w) * col + (j + q)) == 0)
{
*(erosion_img + i * col + j) = 2;
continue;
}
}
}
}
}
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (*(erosion_img + i * col + j) == 2)
{
*(erosion_img + i * col + j) = 0;
}
}
}
}
void opening(int *img, int *opened_img, int *structure, int row, int col, int x, int y)
{
erosion(img, opened_img, structure, row, col, x, y);
dialation(img, opened_img, structure, row, col, x, y);
}
void closing(int *img, int *closed_img, int *structure, int row, int col, int x, int y)
{
dialation(img, closed_img, structure, row, col, x, y);
erosion(img, closed_img, structure, row, col, x, y);
}
I am trying to use it but it is not executing properly.
#include <stdio.h>
#include <stdlib.h>
float average_score(int** array_score)
{
int i, j;
int sum = 0;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
sum += *(*(array_score + i) + j);
}
}
printf("sum / 16 = %d\n", sum / 16);
return (float) sum / 16;
}
int lowest_score(int** array_score)
{
int i, j;
int temp = **array_score;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(temp > *(*(array_score + i) + j))
//------
temp = *(*(array_score + i) + j);
printf("%d ",*(*(array_score + i) + j));
}
//--
printf("\n");
}
printf("low_score = %d\n", temp);
return temp;
}
int main(int argc, char** argv)
{
int **array_score = NULL;
int i = 0;
int j = 0;
//Create a two-dimensional array
array_score = (int **) malloc(4 * sizeof(int *));
for(i = 0; i <= 4; i++)
array_score[i] = (int *)malloc(4 * sizeof(int));
//--
for(i = 0; i < 4; i++)
{
printf("Please enter the student_%d four grades, (separated with a space )\n", i+1);
int ret = scanf("%d %d %d %d", (*(array_score + i) + 0), (*(array_score + i) + 1),
(*(array_score + i) + 2), (*(array_score + i) + 3));
fflush(stdin);
if(4 != ret)
i--;
}
//There is something wrong with the function return value
float ave_score = average_score(array_score);
//--
int low_score = lowest_score(array_score);
//The output
printf("average score: %d\n lowest score: %d\n", ave_score, low_score);
return 0;
}
This allocates space for 4 pointers and then writes 5, since i takes on the values 0, 1, 2, 3 and 4:
array_score = (int **) malloc(4 * sizeof(int *));
for(i = 0; i <= 4; i++)
array_score[i] = (int *)malloc(4 * sizeof(int));
Note that in C you shouldn't cast the return from malloc. There's an answer on Stackoverflow telling all the details why this is not helpful.
So i have assignment to type a code which will ask me for a "w". After i type in a number it will create a rhombus with a diagonal which is 2w. The rhombus must be made of intervals and * . The problem that i face now is that when i type w=5 the diagonal is 5 instead of 10 ....
main()
{
int w;
int i;
int j;
printf("w: ");
scanf("%d", &w);
printf("");
i = 0;
while (w >= i)
{
for (j = 0; j < (w - i); j++)
printf(" ");
for (j = 0; j < i + 1; j++) {
printf("*");
if (j <= i) {
printf(" ");
}
}
printf("\n");
i = i + 1;
}
i = w - 1;
while (i >= 0)
{
for (j = 0; j < (w - i); j++)
printf(" ");
for (j = 0; j < i + 1; j++) {
printf("*");
if (j <= i) {
printf(" ");
}
}
printf("\n");
i = i - 1;
}
return 0;
}
If you add the line w = 2*(w-1) + 1; before any of the loops then you get the correct number of *s to print out ( I just looked for the pattern you were getting and modified the input)
You can also do this problem with just one loop!
Edit:
#include <stdio.h>
#include <math.h>
#define min(a, b) (((a) < (b)) ? (a) : (b))
int main(){
int input, row, column;
printf("input a number: ");
scanf("%d", &input);
printf("");
input = 2*(input-1) + 1;
int pivot = input;
int total_spaces = input*2+1;
for(row = 0; row < total_spaces; row++){
for(column = 0; column < total_spaces; column ++){
if(min(abs(total_spaces - row -1),abs(0 - row)) +
min(abs(total_spaces - column -1),abs(0 - column))
>= input && (row%2 != column %2)){
printf("*");
}
else printf(" ");
}
printf("\n");
}
}
That was a doozy!
I ran your program and I had this :
/a.out
w: 5
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
I do not see where your diagonal is 5. Can you be more specific ?
Also, I understand how this may not be important for you but as-is your code won't compile.
At least add int before your main function.
This program is for adding two polynomials and printing the result.
It just stores the multipliers in array and use the index az the power of parameter
It does sum and subtract but when it comes to product it prints
0x0 0*x2 0*x3 ...
It's for basic C programming class and in three hours I should give it to master :-(
It gets the multiplier in an array and calculates the result using some functions:
#include <stdio.h>
#include <stdlib.h>
double a[50], b[50], c[101];
int dega, degb;
SumArray (a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i, max = (dega < degb ? degb : dega) + 1;
for(i = 0; i < max; i++)
c[i] = a[i] + b[i];
}
SubtractArray(a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i, max = (dega < degb ? degb : dega) + 1;
for (i = 0; i < max; i++)
c[i]=a[i]-b[i];
}
ProductArray(a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i,j;
double tempa, tempb;
for(i = 0; i < dega + 1; i++)
for(j = 0; j < degb + 1; j++)
{
tempa = a[i];
tempb = b[j];
c[i + j] = c[i + j] + (tempa * tempb);
}
}
int main()
{
extern int dega, degb;
extern double a[50], b[50], c[]; //stores the multipliers
int i, operation;
for(i = 0; i < 50; i++)
a[i] = b[i] = 0;
for(i = 0; i < 102; i++)
c[i] = 0;
printf("darjeye chand jomleee ha ra vared konid");
scanf("%d %d", °a, °b);
printf("zarayebe chand jomlee aval ra vaerd konid");
for(i = 0; i < dega + 1; i++)
scanf("%d", &a[i]);
printf("zarayebe chand jomlee dovoom ra vaerd konid");
for(i = 0; i < degb + 1; i++)
scanf("%d", &b[i]);
printf("amaliyate morede nazartan ra vare konid baraye jame 0, tafrigh 1, zarb 2 ra vared konid");
scanf("%d", &operation);
switch(operation)
{
case 0:
{
SumArray(a,b);
for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
{
printf(" %d*x", c[i]);
printf("%d ", i);
}
break;
}
case 1:
{
SubtractArray(a,b);
for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
{
printf(" %d*x", c[i]);
printf("%d ", i);
}
break;
}
case 2:
{
ProductArray(a,b);
for(i = 0; i < (dega + degb + 1); i++)
{
printf("\%d*x", c[i]);
printf("%d ", i);
}
break;
}
default:
printf("amaliyate vared shode sahih nabud");
}
system("PAUSE");
return 0;
}
scanf("%d",a[i]); should be scanf("%d",&a[i]); and scanf("%d",b[i]); should be scanf("%d",&b[i]);
scanf requires address of the variable. For an array, say, in your case double a[50], simply writing a gives you the starting address of array a which will be same as &a[0] where a[0] is simply the first element, but not the address to the first element. a[i] is the element where &a[i] is the address to that element. Hope you got it.