I need that vector_total dont have any repeated number.
Function for entry vector1 and vector2 that are declared in main().
void entrada_vectors(int vector1[], int vector2[], int vector_total[], int *n, int *m)
{
int i=0, j=0;
/*Entrarem els nombres del vector 1 primer */
for (i=0; i<*n; i++)
{
vector_total[i]=vector1[i];
}
/*Entrarem els nombres del vector 2 després */
for (i=*n; i<*n+*m; i++)
{
if (j<*m)
{
vector_total[i]=vector2[j];
j++;
}
}
}
Function 2. This is for order numbers in vector_total.
void ordena(int vector_total[], int *n, int *m)
{
int i=0, j=0;
int aux=0;
for (i=0; i<*n+*m-1; i++)
{
for (j=0; j<*n+*m-1; j++)
{
if (vector_total[j]>vector_total[j+1])
{
aux=vector_total[j];
vector_total[j]=vector_total[j+1];
vector_total[j+1]=aux;
aux=0;
}
}
}
}
Function 3. Print vector_total
void mostra(int vector_total[], int *n, int *m )
{ int i;
for (i=0; i<*n+*m; i++)
{
printf ("Pos %d del vector: %d\n", i, vector_total[i] );
}
}
Function 4. Here are the problem!! This function is for clean my vector_total and drop the repeated numbers.
void limpiar_repetidos(int vector_total[], int *n, int *m)
{
int x=0, i=0, j=0;
for (i=0; i<*n+*m-1; i++)
{
for (j=0; j<*n+*m-1; j++)
{
if (vector_total[j]==vector_total[j+1])
{
x=j+1;
for (i=*n+*m; i>x; i--)
{
vector_total[i-1]=vector_total[i];
}
}
}
}
}
And here is my main. And my declaration variables:
int vector1[]={7,1,5,3,4,2};
int vector2[]={3,7,3,0,9,10};
int n=sizeof(vector1)/sizeof(vector1[0]);
int m=sizeof(vector2)/sizeof(vector2[0]);
int vector_total[n+m];
main()
{
entrada_vectors(vector1, vector2, vector_total, &n, &m);
ordena(vector_total, &n, &m);
mostra(vector_total, &n, &m);
limpiar_repetidos(vector_total, &n, &m);
printf ("==================\n");
mostra(vector_total, &n, &m);
return 0;
}
Thanks everybody! :)
1) If function deal with only a vector_total , Length of the vector_total is better to pass by one argument. Also you do not need to pointer pass if it will not change.
void mostra(int vector_total[], int len){
int i;
for (i=0; i<len; i++) {
printf ("Pos %d del vector: %d\n", i, vector_total[i] );
}
}
2) function need a new length after the element has been removed. Also, deletion of adjacent same elements that can be like this.
int limpiar_repetidos(int vector_total[], int len){//int *n, int *m --> int len
int i, size, new_size;
size = len;
for(i = new_size = 1; i < size; ++i){
if(vector_total[new_size-1] != vector_total[i])
vector_total[new_size++] = vector_total[i];
}
return new_size;
}
Example of use
mostra(vector_total, m + n);
int l = limpiar_repetidos(vector_total, n + m);
mostra(vector_total, l);
Related
I am trying to create a program to find the transpose of a matrix my dynamic memory allocation. However, while entering the elements of the matrix I can't input more than one element, its only taking the a[0][0] as the input and the program is ending after that.
#include <stdio.h>
#include <stdlib.h>
void createMatrix(int **, int, int);
void inputElements(int **, int, int);
void transpose(int **, int **, int, int);
void display(int **, int, int);
void main()
{
int **matrix, **trans, rows, cols;
printf("\nEnter number of rows in the matrix: ");
scanf("%d", &rows);
printf("\nEnter number of columns in the matrix: ");
scanf("%d", &cols);
createMatrix(matrix, rows, cols);
createMatrix(trans, cols, rows);
inputElements(matrix, rows, cols);
transpose(matrix, trans, rows, cols);
printf("\nMATRIX:\n");
display(matrix, rows, cols);
printf("\nTRANSPOSE OF THE MATRIX:\n");
display(trans, rows, cols);
}
void createMatrix(int **a, int r, int c) //for allocating memory for the matrix
{
int i, j;
a = (int **)malloc(sizeof(int *) * r);
for(i = 0; i < r; i++)
a[i] = (int *)malloc(sizeof(int) * c);
}
void inputElements(int **a, int r, int c) //for entering matrix elements
{
int i, j, t;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("\nEnter matrix element[%d][%d]: ", i + 1, j + 1);
fflush(stdin);
getchar();
scanf("%d", &(a[i][j]));
}
}
}
void transpose(int **a, int **t, int r, int c) //for finding out the transpose of the matrix
{
int i, j;
for (i = 0; i < c; i++)
{
for (j = 0; j < r; j++)
t[i][j] = a[j][i];
}
}
void display(int **a, int r, int c) //for displaying the matrix
{
int i, j;
for (i = 0; i < r; i++)
{
printf("\n");
for (j = 0; j < c; j++)
printf("\t%d", a[i][j]);
}
}
There are many posts about scanf in loops I've seen, but I wasn't able to connect my issue with any of them.
i'm trying to create a function that sets to 0 the value of every matrix element a[i][j] which is divisible by both i and j. I tried to do it this way but the program just "exits" without giving any error or warning after the matrix=editMat(matrix, nrows, ncols); line.
#include <stdio.h>
#include <stdlib.h>
FILE *openFileR(const char *);
int rows(FILE *);
int cols(FILE *);
int **readMat(FILE *, int **);
int **allocMat(int, int);
void printMat(int **, int, int);
int **editMat(int, int, int**);
int main()
{
FILE *fp, *fpout;
fp=openFileR("matrixmag.txt");
int nrows=rows(fp); int ncols=cols(fp);
int **matrix=allocMat(nrows, ncols);
readMat(fp, matrix);
printMat(matrix, nrows, ncols);
matrix=editMat(nrows, ncols, matrix);
printMat(matrix, nrows, ncols);
return 0;
}
int **editMat(int nrows, int ncols, int **matrix )
{
int i, j;
for(i=0; i<nrows; i++)
{
for(j=0; j<ncols; j++)
{
if((matrix[i][j])%i==0 && (matrix[i][j])%j==0)
{
matrix[i][j]=0;
}
}
}
return matrix;
}
int **readMat(FILE *fp, int **matrix)
{
int value, row, col;
while(fscanf(fp, "%d %d %d", &value, &row, &col)!=EOF)
{
matrix[row][col]=value;
}
return matrix;
}
int **allocMat(int nrows, int ncols)
{
int **matrix=malloc(nrows*(sizeof(int *)));
for(int i=0; i<nrows; i++)
{
matrix[i]=malloc(ncols*sizeof(int));
}
return matrix;
}
void printMat(int **matrix, int nrows, int ncols)
{
for(int i=0; i<nrows; i++)
{
for(int j=0; j<ncols; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int cols(FILE *fp)
{
int columns;
fscanf(fp, "%d", &columns);
printf("Columns:%d\n", columns);
return columns;
}
int rows(FILE *fp)
{
int lines;
fscanf(fp,"%d", &lines);
printf("Rows:%d\n", lines);
return lines;
}
FILE *openFileR(const char *nome_file)
{
FILE *fp;
printf("File name: %s\n", nome_file);
fp=fopen(nome_file, "r");
if(fp==NULL)
{
printf("Couldn't open file\n");
exit(-1);
}
else
printf("File correctly opened\n");
return fp;
}
Input file is
3 3
2 0 0
7 0 1
6 0 2
9 1 0
5 1 1
1 1 2
4 2 0
3 2 1
8 2 2
sorry about the errors, i didn't check what i wrote before
Just figured out i was trying to divide a number by 0, since i and j were both initialized to 0
int **editMat(int nrows, int ncols, int **matrix )
{
int i, j;
for(i=0; i<nrows; i++)
{
for(j=0; j<ncols; j++)
{
if((matrix[i][j])%i==0 && (matrix[i][j])%j==0)
{
matrix[i][j]=0;
}
}
}
return matrix;
}
This way it works fine
int **editMat(int nrows, int ncols, int **matrix )
{
int i, j;
for(i=1; i<nrows; i++)
{
for(j=1; j<ncols; j++)
{
if((matrix[i][j])%i==0 && (matrix[i][j])%j==0)
{
matrix[i][j]=0;
}
}
}
return matrix;
}
Thanks everybody for the help anyway :)
If I print result inside my function, I get the correct answer but if I print the result in main, I get 1. What am I doing wrong?
#include <stdio.h>
int dotpro(int v1[], int v2[], int result, int n);
int main(void) {
int i, n;
printf("Enter n: ");
scanf("%d", &n);
int v1[n], v2[n], result;
printf("Enter arr1: ");
for(i= 0; i < n; i++)
scanf("%d", &v1[i]);
printf("Enter arr2: ");
for(i= 0; i < n; i++)
scanf("%d", &v2[i]);
dotpro(v1, v2, result, n);
// enter code here
}
int dotpro(int v1[], int v2[], int result, int n) {
int i;
for (i = 0; i < n; i++) {
result += (v1[i] * v2[i]);
}
printf("%d", result);
}
You are forgetting to return the result and are attempting to pass the result parameter by value instead of by pointer reference.
Instead of this:
int dotpro(int v1[], int v2[], int result, int n) {
int i;
for (i = 0; i < n; i++) {
result += (v1[i] * v2[i]);
}
printf("%d", result);
}
Do this:
int dotpro(int v1[], int v2[], int n) {
int i;
int result = 0;
for (i = 0; i < n; i++) {
result += (v1[i] * v2[i]);
}
return result;
}
And adjust the declaration at the top of the file to match:
int dotpro(int v1[], int v2[], int n);
Then in main, invoke as follows:
result = dotpro(v1, v2, n);
printf("result = %d\n", result);
I feel like I've attempted every combination I know of to get this to work and can't figure it out. How can I scanf() into an int** passed as a pointer to a function? I tried searching but couldn't find this, if it's a duplicate please let me know and I'll delete. It begins to run and after entering a few values it segfaults.
Here's my code, I think it's messing up on the scanf() line of the setMatrix() function:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int ***mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", mat[i][j]); // problem here??
printf("matrix[%d][%d]: %d\n", i, j, (*mat)[i][j]);
}
}
return;
}
// print matrix
void printMatrix(int ***mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", (*mat)[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(&mat, r, c);
printMatrix(&mat, r, c);
}
There is no need to use triple pointer ***. Passing two-dimensional array will work as is. Here is the code:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int **mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", &mat[i][j]); // no problem here
printf("matrix[%d][%d]: %d\n", i, j, mat[i][j]);
}
}
}
// print matrix
void printMatrix(int **mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(mat, r, c);
printMatrix(mat, r, c);
}
Should be:
scanf("%d", &(*mat)[i][j]);
You're passing a pointer to you matrix object, so you need to dereference it (with *) just as you do with printf. scanf then needs the address of the element to write into, so you need the &
I have two 2d arrays "a", "b" (empty array) with the same size, I have to change "a" due to a certain function that save it's new values in "b", then I have to change the new values according to the same function, so the program will save b's new values in a and then back to a.
When the arrays are printed only the first two ones are printed!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MSIZE 10
void new_gen(char * a[MSIZE],int s,char** b); /*the function we talked about*/
void print_m(char** b,int s); /*prints matrix*/
void cpy_m(char** b, char** a, int s);
int main()
{
int Size, gen, i, j;
printf("Enter number of generations\t");
scanf("%d", &gen);
printf("\nEnter size of the matrix (max size is %d and min is 2)\t", MSIZE);
scanf("%d", &Size);
char **m = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
m[i] = (char*) malloc(Size*sizeof(char));
}
printf("Enter matrix of first generation\n");
for (i=0; i<Size; i++) {
for (j=0; j<Size; j++) {
scanf(" %c", &m[i][j]);
}
}
print_m(m, Size);
for (i=1; i<gen; i++)
{
char **n = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
n[i] = (char*) malloc(Size*sizeof(char));
}
new_gen(m, Size, n);
print_m(n, Size);
cpy_m(n, m, Size);
}
return 0; }
void print_m(char** b, int s)
{
int i, j;
putchar('\n');
for (i=0; i<s; i++)
{
for (j=0; j<s; j++) {
printf("%c", *(*(b+i)+j));
}
putchar('\n');
}
return;
}
void cpy_m(char* b[MSIZE],char** a, int s)
{
int i, j;
for (i=0; i<s; i++) {
for (j=0; j<s; j++) {
*(*(a+i)+j) = b[i][j];
}
return;
}}
Consider this pair of nested loops
for (i=1; i<gen; i++)
{
char **n = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
n[i] = (char*) malloc(Size*sizeof(char));
}
new_gen(m, Size, n);
print_m(n, Size);
cpy_m(n, m, Size);
}
First point, both the loops use i as the control variable, but they are nested.
Second point, you overwrite the pointers from malloc in each iteration (there is no free).