I want to write a code (in c) that prints the intersection of two vectors of "N" lenght and cannot print the same number twice. The vectors will be filled with a function, that reads the input ( any number), one by one. And after all, the code needs to print another vector with the intersection between the first two vectors. Until now i wrote the code below, but it has a logical error that i cant solve.
#include <stdlib.h>
#include <stdio.h>
void preencheVetor(int* v, int tamanho){
int i=0;
for(i=0;i<tamanho;i++){
scanf("%d", *(v+i));
printf("\n");
}
}
void interVetor(int* v1, int* v2, int tamanho){
int* v3=malloc(sizeof(int)*tamanho);
int i1=0;
int i2=0;
int i3=0;
int c=0;
for(i1=0;i1<tamanho;i1++){
for(i2=0;i2<tamanho;i2++){
if((v1[i1])==(v2[i2])){
*(v3+c)=v2[i2];
c++;
}
}
}
for(i3=0;i3<tamanho;i3++){
printf("%d", *(v3+i3));
printf("\n");
}
}
int main(){
int n=0;
printf("Vectors Lenght:\n");
scanf("%d", &n);
printf("\n\n");
int v1[n];
int v2[n];
preencheVetor(v1, n);
printf("\n\n");
preencheVetor(v2, n);
printf("\n\n");
interVetor(v1, v2, n);
system("PAUSE");
return 0;
}
The size of vector v3 is c not tamanho. Fix the for(i3...) loop.
Other than that, I think your intersection works OK.
It is very inefficient, of course, if the vectors are not sorted.
Related
It successfully compiles.
But at running on getting values of matrix it crashes stops working.
#include <stdio.h>
void getmat(int mat[100][100],int m,int n);
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int
n2,int matmul[100][100]);
void printmat(int matmul[100][100],int m,int n);
int main(void)
{
int m1,n1,m2,n2;
printf("Enter the dimensions of matrix1: ");
scanf("%d %d",&m1,&n1);
printf("Enter the dimensions of matrix2: ");
scanf("%d %d",&m2,&n2);
int mat1[m1][n1];
int mat2[m2][n2];
int matmul1[m1][n2];
int matmul2[m2][n1];
printf("For the values of matrix 1\n");
getmat(mat1,m1,n1);
printf("For the values of matrix 2\n");
getmat(mat2,m2,n2);
if(n1==m2)
{
printf("Mat1 x Mat2 is possible.");
matmul(mat1,mat2,m1,n1,m2,n2,matmul1);
printf("Mat1 x Mat2 :\n");
printmat(matmul1,m1,n2);
}
else
printf("Mat1 x Mat2 is not possible.\n");
if(n2==m1)
{
printf("Mat2 x Mat1 is possible.");
matmul(mat2,mat1,m2,n2,m1,n1,matmul2);
printf("Mat2 x Mat1 :\n");
printmat(matmul2,m2,n1);
}
else
printf("Mat2 x Mat1 is not possible.\n");
return 0;
}
void printmat(int matmul[100][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d ",matmul[i][j]);
}
printf("\n");
}
}
void getmat(int mat[100][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter element of %dx%d: ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
}
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int
n2,int matmul[100][100])
{
int i,j,k;
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
matmul[i][j]=0;
for(k=0;k<m2;k++)
{
matmul[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
}
Help making any changes or optimizing this code.
Also another way to this.
This error shows up at running a half
Use a Debugger
Also, all your functions are written to take a 100x100 matricies.
But you declare your matricies to have variable sizes:
int mat1[m1][n1];
int mat2[m2][n2];
int matmul1[m1][n2];
int matmul2[m2][n1];
When you pass a 3x3 matrix to a function that is expecting a 100x100 matrix, you will definitely have a bad time.
I see that you are setting up your arrays with variable sizes. You should really set it up as a very large array or use malloc to set up the array properly. When you call the processing routines, pass the arrays as pointers and use the sizes as arguments in order to set up your references. You should note that myarray[i, j] is the equivalent of a single valued array using i*rowsize + j. When you define your array in the subroutines as [100, 100], it will go to [i*100 + j] which is outside the bounds of your actual array.
This causes the program to crash.
Once you actually calculate everything properly, it should work.
I don t get any errors at compilation. The program just crashes when I run it. I tried to print the matrix directly from the generate function and it printed the first line and a bit of the second.
This is my code
void generate(int **a)//*Function to generate a matrix a[i][j]=i+j*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
a[i][j]=i+j;
}
}
}
void print(int **a)//*print the resulting matrix from generate function*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
}
int main()
{
int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
generate(&a);
print(&a);
}
1) you are allocating a single dimension memory.
a[i][j]=i+j; //is not valid.
Below is the modified code
#include <stdio.h>
#include <stdlib.h>
void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
*a=i+j;
a++; //increments to next memory location
}
}
}
void print(int *a)//*print the resulting matrix from generate function*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d ",*(a++)); //notice another way of accessing
}
printf("\n");
}
}
int main()
{
int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
generate(a);
print(a); //passing the pointer
free(a); //Always always practice to free the allocated memory, don't ever do this mistake again
return 0;
}
Two things:
First, int ** denotes a pointer to a pointer that points to an int, not a pointer that points to an array of ints.
Second, when you just pass a pointer to some data structure, e.g. an 4x5 array of integers, then the compiler cannot derive the layout of this data structure. I.e. a statement like a[i][j] would require that the compiler "knows" that each row i consists of 4 columns j, such that it can calculate the "place" to which the value should be stored, i.e. a + (4*i) + j. The compiler simply does not know the nr of columns per row, i.e. 4.
To overcome this while keeping the size of the array at least potentially variable (note that "4" and "5" are still hard coded in the function), you could do the following:
void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
*(a+(i*4+j)) = i+j;
}
}
}
void print(int *a)//*print the resulting matrix from generate function*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d ", *(a+(i*4+j)));
}
printf("\n");
}
}
int main()
{
int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
generate(a);
print(a);
}
So I'm really new to programming. I need to write a program that, if I give it any array of integers, it'll be able to find the two numbers closest to each other, and then give the difference between those two numbers. Also, the first number must be the number of integers that are going to be in the array.
So for example, I give it 3 1 4 8. The first 3 means that there will be three integers, so it must find the closest two numbers between those three. In this case, it's 4 - 1 = 3, so the output should be 3, but when I write it it gives me 16.
This is what I have, and I don't know what's wrong:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, str[n*n], minimum, c;
/* here I'll make a new array, and its elements will be all the
differences between all the elements of the previous one */
for(a=0;a<n;a++)
for(b=0+a*n;b<n;b++) {
if(st[b-a*n]==st[a])
str[b]=32000;
else
str[b]=abs(st[b-a*n]-st[a]);
}
// here I'll find the smallest element on the last made array
minimum = str[0];
for(c=0;c<n*n;c++)
{
if(str[c]<minimum);
{
minimum=str[c];
}
}
printf("%d", minimum);
return 0;
}
Edit: I tried to fix it with your answers but it still doesn't work.
New code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = st[0];
for(a=0;a<n;a++)
for(b=0;b<n;b++) {
if((st[b] != st[a]) && (abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
Edit 2: Ok, I fixed it now. Thanks a lot ^^
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int n, i;
printf("write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = INT_MAX;
for(a=0;a<n;a++)
for(b=a+1;b<n;b++) {
if((abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
i have this code how to read the bidimensional array using a function?
i write this function it works read all the numbers but when i output to console the array there are not the values that i entered
ex
Input:
2 1 2 3 4
Output:
16 256
1 4525376
#include <stdio.h>
#include <stdlib.h>
void citMat(int a, int n) {
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("a[%d][%d]",i,j);
scanf("%d", &a);
}
}
int main()
{ int i,j;
int a[10][10],n;
printf("Introdu n:");
scanf("%d", &n);
citMat(a[10][10],n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
You need to change the prototype to (Here array dimension is important)
void citMat(int a[10][10], int n)
Other changes are explained by others (The whole code is below)
#include <stdio.h>
#include <stdlib.h>
void citMat(int a[10][10], int n) {
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]:",i,j);
fflush(stdout);
scanf("%d", &a[i][j]);
}
}
int main()
{ int i,j;
int a[10][10],n;
printf("Introdu n:");
scanf("%d", &n);
if (n > 10)
{
fprintf(stderr, "Invalid input %d\n", n);
return 1;
}
citMat(a,n);
for(i=0;i<n;i++){
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
1. If you want to pass a 2-d array to function .Change your function definition to -
void citMat(int a[10][10], int n) { // first parameter to take a 2-d int array
2. And then inside function citMat to take input-
scanf("%d", &a[i][j]); // you need to write like this
Note -
1. Array indexing starts from 0 , so if you have array a[n] then it have valid index from 0 to n-1 .
So start reading from 0 and till n in all for loops . If you include n then you would access index out of bound and writing to it will cause undefined behaviour.
So, look out for that .
2. int main() -> int main(void) or int main(int argc,char **argv)
You need to change few things in your program to make it work
1) Call the function with the base address of the array, lik
citMat(a,n);
2) Change your function definition to,
void citMat(int a[10][10], int n)
to make it accept 2D array as parameter.
3) Change the scanf() to read for each element,
scanf("%d", &a[i][j]);
4) Since the array index starts from 0, change all the for loops termination condition to
for(i=1;i<n;i++)
I'm new in C programming language, and I try to practice my skills in this language.
I'm coding an exercise about a matrix, where the user inputs the number of the column that wants to sort and print the column sorted (I'm using the bubble sort), but the program doesn't show me the answer.
This is my implementation of the problem.-
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void bubble(int *array);
void print(int *array);
int main(){
/*Este programa toma una columna y la ordena*/
int fil=4, col=4;
int matrix[fil][col];
for(int i=0;i<fil;i++){
for(int j=0;j<col;j++){
matrix[i][j]=rand()%10+1;
}
}
for(int i=0;i<fil;i++){
for(int j=0;j<col;j++){
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
int a, aux[col];
printf("\nColumna a ordenar: ");
scanf("%d",&a);
for(int i=0;i<fil;i++){
for(int j=0;j<col;j++){
if(j==a){
aux[j]=matrix[0][j];
}
bubble(aux);
}
}
getche();
}
void bubble(int *array){
int length= sizeof(array)/sizeof(array[0]);
int aux;
for(int i=length-2;i>=0;i--){
for(int j=0;j<=i;j++){
if(array[j]>array[j+1]){
aux=array[j];
array[j]=array[j+1];
array[j+1]=aux;
print(array);
}
}
}
}
void print(int *array){
int length= sizeof(array)/sizeof(array[0]);
for(int i=0;i<length;i++){
printf("%d",array[i]);
}
}
And, which book or tutorial should i follow to master the c language (C)?
First : using
int length= sizeof(array)/sizeof(array[0]);
cannot give you the number of elements of an array. Indeed, sizeof(array) will give you the size in memory of the pointer.
In C/C++, you must pass the number of elements of your arrays as an argument for the function using it. So your functions should be like :
void bubble(int *array, int size);
void print(int *array, int size);
Second : your loop
for(int i=0;i<fil;i++){
for(int j=0;j<col;j++){
if(j==a){
aux[j]=matrix[0][j];
}
bubble(aux);
}
}
is unlikely to fill the aux array properly. Maybe something like :
int a, aux[fil];
printf("\nColumna a ordenar: ");
scanf("%d",&a);
for(int i=0;i<fil;i++){
aux[i] = matrix[i][a];
}
bubble(aux, fil);
print(aux, fil);
rather than this double loop ?
Look at the third "for" loop in your main().
for(int i=0;i<fil;i++){
for(int j=0;j<col;j++){
if(j==a){
aux[j]=matrix[0][j]; //Should be aux[i]=matrix[i][j] here, or you're actually putting everything in aux[a], since j==a.
}
bubble(aux);
}
}
Also, you should define aux as aux[fil]. Since fil==col, it's ok here. But aux is intended to be used to store a column instead of a row, so if your fil isn't eqaul to col, there would be problems.
You can write it like this:
int aux[fil];
for(int i=0;i<fil;i++)
aux[i]=matrix[i][a];
bubble(aux);
As for which book, I can recommend nothing other than Kernighan and Ritchie's "The C Programming Language".