I am tryin to take input from the user and store it into an array and print it out: i have 2 functions:
/* Read a vector with n elements: allocate space, read elements,
return pointer */
double *read_vector(int n){
double *vec = malloc(n * sizeof(double));
int i;
for (i = 0; i < n; i++)
vec[i] = n;
return vec;
}
and the print function is:
void print_vector(int n, double *vec){
int i;
for (i = 0; i < n; i++) {
printf("%d\n", vec[i]);
}
}
the main function is:
#include <stdio.h>
#include <stdlib.h>
double *read_vector(int n);
void print_vector(int n, double *vec);
void free_vector(double *vec);
int main(){
int n;
double *vector;
/* Vector */
printf("Vector\n");
printf("Enter number of entries: ");
scanf("%d", &n);
printf("Enter %d reals: ", n);
vector = read_vector(n);
printf("Your Vector\n");
print_vector(n,vector);
free_vector(vector);
}
when i run this, it does not let me enter any numbers, it just skips it and prints out 0's. How do i fix this?
Try the code below. You're almost certainly either not compiling with warnings, or ignoring the warnings. All warnings mean something, and to a beginner they all matter. With gcc use the -Wall option, or even -pedantic.
As halfelf pointed out, you need a scanf in your read loop but it needs to be a pointer (&vec[i]). Always return something at the end of main. Also check the return value of malloc, it could fail and return a null pointer.
#include <stdio.h>
#include <stdlib.h>
double *read_vector(int n)
{
double *vec = malloc(n * sizeof(double));
int i;
for (i = 0; i < n; i++) {
printf("Enter number %i of %i: ", i + 1, n);
scanf("%lf", &vec[i]);
}
return vec;
}
void print_vector(int n, double *vec)
{
int i;
for (i = 0; i < n; i++) {
printf("%f\n", vec[i]);
}
}
void free_vector(double *vec)
{
free(vec);
}
int main()
{
int n;
double *vector;
printf("Vector\n");
printf("Enter number of entries: ");
scanf("%i", &n);
vector = read_vector(n);
printf("Your Vector\n");
print_vector(n, vector);
free_vector(vector);
return 0;
}
In the read_vector(int n) function's for loop:
for (i=0; i<n; i++)
vec[i] = n; // this should be scanf("%lf",vec+i) to read input from stdin
and notice your { and } there. If there's only one line in the loop, { and } is not necessary, OR you have to use a pair of them. The return clause must be out of the loop.
Btw, add return 0 at the end of your main function.
simple..you forgot to write scanf..!
double *read_vector(int n)
{
double *vec = malloc(n * sizeof(double));
int i;
for (i = 0; i < n; i++)
scanf("%d",&vec[i]);
return vec;
}
Related
This question already has answers here:
Changing address contained by pointer using function
(5 answers)
Closed 2 years ago.
In the below code I am trying to scanf() a vector with dynamic dimension (entered by the user) using a secondary function. I am not getting any errors or warnings, but the vector is not getting printed from main(). Any ideas on what I am missing? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
void save_vector(int dim, float *u);
int main()
{
float *v = 0;
int i, dim;
setlocale(LC_CTYPE, "spanish");
printf("Please enter the vector dimension:\n");
scanf("%d", &dim);
save_vector(dim, v);
for (i = 0; i < dim; ++i)
{
printf("%f\n", v[i]);
}
return 0;
}
void save_vector(int dim, float *u)
{
int i;
u = (float *)calloc(dim, sizeof(float));
for (i = 0; i < dim; ++i)
{
scanf("%f", &u[i]);
}
}
As you know when you want to make the changes permanent to a variable passed in a function argument you need to pass a pointer to it.
In this case you want to change a pointer so you need to pass a pointer to that pointer.
I would suggest a simpler approach which is to return the pointer instead of passing it by argument:
Live demo
int main()
{
float *v;
int dim;
printf("Please enter the vector dimension:\n");
scanf("%d", &dim);
v = save_vector(dim); // assign the variable allocated in the function
for (int i = 0; i < dim; ++i)
{
printf("%f\n", v[i]);
}
return 0;
}
float* save_vector(int dim) //pointer to float return type
{
int i;
float *u;
u = calloc(dim, sizeof *u);
if(u == NULL) {
exit(EXIT_FAILURE);
}
for (i = 0; i < dim; ++i)
{
scanf("%f", &u[i]);
}
return u; //return allocated array
}
If you really want to use a double pointer you can use something like this:
Live demo
int main() {
float *v;
int dim;
printf("Please enter the vector dimension:\n");
scanf("%d", &dim);
alloc_vector(dim, &v); //allocate vector
save_vector(v, dim); //having enough memory assing the values
print_vector(v, dim); //print the vector
return EXIT_SUCCESS;
}
void alloc_vector(int dim, float **u) { //double pointer argument
*u = calloc(dim, sizeof **u)); //allocate memory for the array
if(*u == NULL) { //allocation error check
exit(EXIT_FAILURE);
}
}
void save_vector(float *u, int dim) {
for (int i = 0; i < dim; ++i) {
scanf("%f", &u[i]);
}
}
void print_vector(float *u, int dim){
for (int i = 0; i < dim; ++i)
{
printf("%f\n", u[i]);
}
}
For n=3 and a={1,2,3},b={4,5,6} its supposed to calculate 1*4+2*5+3*6.
I don't understand why does it work because p is a pointer and p=produs(a,b,n) means that the address of p becomes the value returned by produs.
#include <stdio.h>
#include <conio.h>
void citire(int *x,int *n)
{
for(int i=1; i<=*n; i++)
scanf("%d",&x[i]);
}
int produs(int *a,int*b,int n)
{
int produs=0;
for(int i=1;i<=n;i++)
produs=a[i]*b[i]+produs;
return produs;
}
int main()
{
int n;
int*p;
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
int *b=(int*)malloc(n*sizeof(int));
citire(a,&n);
citire(b,&n);
p=produs(a,b,n);
printf("%d",p);
return 0;
}
When you do:
size_t size = 10;
int* x = calloc(size, sizeof(int));
You get an array x with 10 items in it, indexed 0..9, not 1..10. Here calloc is used to make it abundantly clear what's being requested instead of doing multiplication that can be mysterious or obtuse.
As such, to iterate:
for (int i = 0; i < size; ++i) {
x[i] ...
}
You have a number of off-by-one errors in your code due to assuming arrays are 1..N and not 0..(N-1).
Putting it all together and cleaning up your code yields:
#include <stdio.h>
#include <stdlib.h>
void citire(int *x, size_t s)
{
for(int i=0; i < s; i++)
scanf("%d", &x[i]);
}
int produs(int *a, int* b, size_t s)
{
int produs = 0;
for(int i = 0; i < s; i++)
produs = a[i] * b[i] + produs;
return produs;
}
int main()
{
int n;
scanf("%d",&n);
int* a = calloc(n, sizeof(int));
int* b = calloc(n, sizeof(int));
citire(a, n);
citire(b, n);
// produs() returns int, not int*
int p = produs(a,b,n);
printf("%d", p);
return 0;
}
You're using pointers in places where pointers don't belong. In C passing a pointer to a single value means "this is mutable", but you don't change those values, so no pointer is necessary nor advised.
Try and use size_t as the "size of thing" type. That's what's used throughout C and it's an unsigned value as negative indexes or array lengths don't make any sense.
Calculating average three by three elements and replacing those elements with the average result.
Example array [1,2,7,-2,5,0, 2,8]
After transformation [3,3,3,1,1,1,5,5]
Something is wrong, I can't get it to work.
#include <stdio.h>
int main ( ) {
int n, c[n];
int *avg;
int pom=0;
printf("Enter lenght of array\n");
scanf("%d",&n);
printf("Enter elements");
for(i = 0;i < n; i++)
scanf("%d",c[i]);
avg=Average(c , n, pom);
for(i = 0; i < n; i++)
printf("Avg elements= %d",*(avg+i))
return 0;
}
int Average(int arr[], int size, int z)
{
int k, l, m, Asum;
if (size < 0) {
return arr;
} else {
k=arr[z];
l=arr[z+1];
m=arr[z+2];
Asum=(k + l + m)/3;
arr[z]=Asum;
arr[z+1]=Asum;
arr[z+2]=Asum;
}
return Average(arr,size--,z++);
}
int n, c[n]; is a problem. n is uninitialized so the size of the array is who-knows-what? This is undefined behavior.
Instead
int main(void) {
int n;
int *avg;
int pom=0;
printf("Enter length of array\n");
if (scanf("%d",&n) != 1) return -1;
int c[n];
for(i = 0;i < n; i++)
// scanf("%d",c[i]);
scanf("%d",&c[i]); // pass the address of an `int`
Likely other issues too.
Try simple input first, imagine what happens when you enter only 1 number, what will the Average function do? Don't run the code but try to execute it in your head or with pencil and paper. If you think the program only has to work with three or more numbers, try three.
A serious program would explicitly reject invalid input.
Here is my try. I am not totally sure about my manipulations with the pointer. Maybe this is why I am wrong, maybe there is some other case. I want to take the dimensions from the user and create a square matrix, make some manipulations with its elements, and display the original and results to the user. Last time I accomplished this by creating a 100x100 array, and specifying the end of each line, and end of lines by constants. Then I would print all the elements up to this constant. But it does not seem to be right to create a 100x100 array for 4x4 matrices. I could create a smaller array, but this does not seem to be the right solution to the problem. Is there a way in C to create a 2d array exactly the size specified by the users (it will be a square array). Thanks
#include <stdio.h>
#include <stdlib.h>
double * createMatrix(int dimentions);
void drawMatrix(double * matrix);
int main(void)
{
int n, i, j;
system("cls");
system("color 70");
system("pause");
puts("Enter the matrix's dimension");
scanf("%i", &n);
double * pmatrix = createMatrix(n);
for (i = 0; i < n; ++j)
for (j = 0; j < n; ++j)
{
printf("A%i%i: ", i + 1, j + 1);
scanf("%lf", pmatrix[i][j]);
getchar();
}
for (i = 0; i < n; ++i)
{
putchar('\n');
for (j = 0; j < n; ++j)
printf(" %lf ", &pmatrix[i][j]);
}
system("color 08");
return 0;
}
double * createMatrix(int n)
{
const int N = n;
const int N1 = N;
double matrix[N][N];
double * pmatrix = matrix;
return pmatrix;
}
You can create a matrix directly; you don't need a function for that. Replace the code
double * pmatrix = createMatrix(n);
by the regular way of declaring a 2-D array:
double matrix[n][n];
One more way of doing it using pointers.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
double **pmatrix;
int rowsize, colsize, i, j;
printf("Enter the number of rows: ");
scanf("%d",&rowsize);
printf("Enter the number of columns: ");
scanf("%d",&colsize);
//Allocate memory for 2D array
pmatrix = malloc(rowsize*sizeof(double*));
for(i=0;i<rowsize;i++)
{
pmatrix[i] = malloc(colsize*sizeof(int));
}
//Accepting the values
for(i=0;i<rowsize;i++)
{
for(j=0;j<colsize;j++)
{
printf("A %i %i: ", i + 1, j + 1);
scanf("%lf",&pmatrix[i][j]);
}
}
//Printing the values
for(i=0;i<rowsize;i++)
{
for(j=0;j<colsize;j++)
{
printf("%lf\t",pmatrix[i][j]);
}
printf("\n");
}
//Free the memory
for(i=0;i<rowsize;i++)
free(pmatrix[i]);
free(pmatrix);
return 0;
}
I want to make a programm that takes the dimension and numbers of vectors to be sorted based on their length.
Most of the code works but the sort part of the program doesnt.
Basically what I want to do there is: compare the output from the bereken_lengte function from 2 places in the array w. But nothing seems to happen.
Also in the function bereken_lengte, I cant take the roots of the sum after the loop ended.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double bereken_lengte(double *array, int dim)
{
int i, j;
double sum = 0.0;
for(i=0; i<dim; ++i)
sum += pow(array[i],2);
return sum;
}
void swap(double **p, double **q)
{
double *tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
void sort_vector(double *w[] , int num , int dik )
{
int i,dim,j;
dim = dik;
for(i=0;i<num;++i)
for(j = 1+i;j<num;++j)
{
if(bereken_lengte(w[i],dim) > bereken_lengte(w[j],dim) )
swap(&w[i], &w[j]);
}
}
int main (void)
{
int dim, num;
int i, j,k,l;
double **w;
scanf ("%d %d", &dim, &num); /* read N and M */
w = calloc (num, sizeof (double *)); /* allocate array of M pointers */
for (i = 0; i < num; i++)
{
/* allocate space for N dimensional vector */
w[i] = calloc (dim, sizeof (double));
/* read the vector */
for (j = 0; j < dim; j++)
{
scanf ("%lf", &w[i][j]);
}
}
sort_vector(w,num,dim);
for(k=0; k<num; ++k)
{
printf("\n");
for(l=0; l<dim; ++l)
printf("%f ", w[k][l]);
}
return 0;
}
double bereken_lengte(double *array, int dim)
{
unsigned int i;
double sum =0.0;
for(i=0; i<dim; ++i)
sum += pow(array[i],2);
return sum;
}
Just initialise the sum to zero before summing.
BTW I changed i to unsigned. It is IMnsvHO a good habit to use unsigned types for index && size variables (they won't underflow, and if the do, you'll notice it)
UPDATE:
This tries to avoid the int indices and sized, and uses qsort. (rather ugly, because the compare function takes only two elements; don't try this in a multithreaded program ...) Note, I may have rows and columns interchanged, but thats a way of life... gewoon, omdat het kan!:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double bereken_lengte(double *array, size_t dim)
{
size_t i;
double sum=0.0;
for(i=0; i<dim; ++i)
sum += pow(array[i],2);
return sum;
}
/* this is ugly: qsort only allows only two arguments */
static size_t ze_third_argument=0;
int srt_pdbl(void *l, void *r)
{
double **dl = l, **dr = r;
double diff;
diff = bereken_lengte( *dl, ze_third_argument) - bereken_lengte( *dr, ze_third_argument) ;
return (int) diff;
}
void sort_vector(double *w[] , size_t num , size_t dik )
{
ze_third_argument = dik;
qsort(w, num, sizeof *w, srt_pdbl );
}
int main (void)
{
size_t dim, num;
size_t i, j,k,l;
double **w;
scanf ("%zu %zu", &dim, &num); /* read N and M */
w = calloc (num, sizeof *w); /* allocate array of M pointers */
for (i = 0; i < num; i++)
{
/* allocate space for N dimensional vector */
w[i] = calloc (dim, sizeof *w[i] );
/* read the vector */
for (j = 0; j < dim; j++)
{
scanf ("%lf", &w[i][j]);
}
}
sort_vector(w,num,dim);
for(k=0; k<num; ++k)
{
printf("\n");
for(l=0; l<dim; ++l)
printf("%f ", w[k][l]);
}
return 0;
}