General function Input array - c

I can't figure what is wrong here. This is supposed to be a function to read from the user the size of array and then pass it to function to start fill the array
#include <stdio.h>
#include <stdlib.h>
int input_array(int *start, int s_size);
int main()
{
int arr_size;
printf("Please enter the Size of your array: ");
scanf("%d",&arr_size);
int arr[arr_size];
input_array(arr,arr_size);
return 0;
}
int input_array(int *start, int s_size)
{
static int counter=0;
printf("Start fill your array with %d elements: \n\n",s_size);
for(counter=0; counter<s_size; counter++)
{
printf("Input Element : ");
scanf("%d",start[counter]);
printf("\n");
}
return start[0];
}

In you function int input_array(int *start, int s_size) this statement
As start is int * -
scanf("%d",start[counter]); // pass address of variable
you need to pass address of start[counter] because it is of type int .

I found the answer that this line
scanf("%d",start[counter]);
should be
scanf("%d",&start[counter]);
the and '&' operator was missig

Related

C programming parameters

#include <stdio.h>
void getScores(int a, char n[10][15], int s[10]) {
int score;
printf("Enter the number of students: ");
scanf("%d",&a);
for (int i=0; i < a;i++)
{
scanf("%s",n[i]);
scanf("%d",&score);
s[i]=score;
}
}
void printScores(int a, char n[10][15], int s[10] ) {
for (int i=0; i < a;i++)
{
printf("%s", n[a]);
printf(" ");
printf("%d\n",s[a]);
}
}
int main() {
char names[10][15];
int scores[10];
int num;
getScores(num,names,scores);
printScores(num,names,scores);
}
What I am trying to accomplish is have the parameter value of int a from the getScores function to be used in the printScores function as an array length as it is being used in getScores.
The arrays are saving its value when used in the print function but the a value is resetting to an unassigned number 896 when I need it to be what the user enters in the get function. Any tips?
Print scores will not print anything.
void getScores(int *a,
And the call
getScores(&num
You need also change accordingly the functions code

program doesn't read a file properly

For homework I have to write a program where I am typing the string of n integer and then the program that prints the string,calculates the arithmetic mean and makes a new file only with even numbers and at the end prints the new file on screen.
And here are 2 programs
#include<stdio.h>
main()
{
int x,n,i;
FILE *p;
p=fopen("podaci.dat","wb");
printf("n=");
scanf("%d",&n);
fwrite(&n,sizeof(int),1,p);
for(i=0;i<n;i++)
{
printf("x=");
scanf("%d",&x);
fwrite(&x,sizeof(int),1,p);
}
fclose(p);
}
#include<stdio.h>
void stampa(int n,int a[])
{
int i;
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\n");
}
float ars(int n,int a[])
{
int i,s=0;
float ars=0;
for(i=0;i<n;i++)
s+=a[i];
return 1.0*s/n;
}
main()
{
int i,n;
FILE *p,*u;
u=fopen("niz.dat","wb");
p=fopen("podaci.dat","rb");
fread(&n,sizeof(int),1,p);
printf("n=%d\n",n);
int a[n],m=0;
for(i=0;i<n;i++)
{
fread(&a[i],sizeof(int),1,p);
if(a[i]%2==0)
{
m+=1;
}
fwrite(&m,sizeof(int),1,u);
for(i=0;i<n;i++)
if(a[i]%2==0)
{
fwrite(&a[i],sizeof(int),1,u);
}
stampa(n,a);
printf("ars=%.2f",ars(n,a));
fclose(p);
fclose(u);
}
}
When I type more than 2 numbers the program instead of those numbers reads them as 0 and sometimes it adds numbers.
You cannot declare an array a[n] where n is a variable (number that you don't know at compile time but only once you read the file).
You can ether declare a[N] where N is a number big enough defined in with #DEFINE
#include <stdio.h>
#include ...
#DEFINE N 10000
//...
int a[N];
or you have to allocate a[] dynamically
int * a;
a = (int*)malloc(sizeof(int)*n);
You can read more about this here and here

my function inside of c program does not work

I am trying to create 2 separate function in my c program that
First program reads the arrays ( names of photographers and their points) and the second one displays all names and points. (With printf command)
But the program doesn't run my second function.
What is wrong with my function?
Thanks in advance
#include <stdio.h>
`#include <string.h>`
void readdata(char name[15][15],float points[15]);
void printdata(char name[15][15],float points[15]);
int main ()
{
char names[15][15];
float points[15];
readdata(names,points);
printdata(names,points);
return 0;
}
void readdata(char name[15][15],float points[15])
{
int i;
int n;
printf("Please enter the number of photographers ( The value should be less than 15)\n");
scanf("%d",&n);
while(n<0 || n>15)
{
printf("PLEASE ADD NUMBER BETWEEN 1 AND 15\n");
scanf("%d",&n);
}
for(i=0; i<n;i++)
{
scanf("%s%f", name[i],&points[i]);
}
}
void printdata(char name[15][15],float points[15])
{
int i;
int n;
for(i=0; i<n;i++)
{
printf("%s\t", name[i]);
printf("%.f\n", points[i]);
}
}
In your printdata() function, the variable int n; is uninitialized.
The variable n here, is different than the variable n you defined inside your readdata() function. These are local variables and are only accessible from within their respective functions.
readdata() should return n and printdata() should receive it as an argument.
You are using n and i in two different functions and not defining them globally means the i the readdata() is not the same as the i in print data().These are the local variables and local variables are only accessible within the function where you declared them. Use arguments to pass the value in printdata() which will be returned by readdata().
Hope it helps.
Your array size ( which is n value) needs to be defined globally. As #user9849588 said, local variables are only accessible from within their respective functions.
To solve this issue, you need to pass your number of photographers n to readdata and printdata functions.
#include <stdio.h>
#include <string.h>
void readdata(char name[15][15],float points[15], int n);
void printdata(char name[15][15],float points[15], int n);
int main ()
{
char names[15][15];
float points[15];
int size;
printf("Please enter the number of photographers ( The value should be less than 15)\n");
scanf("%d",&size);
while(size<0 || size>15)
{
printf("PLEASE ADD NUMBER BETWEEN 1 AND 15\n");
scanf("%d",&size);
}
readdata(names,points,size);
printdata(names,points,size);
return 0;
}
void readdata(char name[15][15],float points[15],int n)
{
int i;
for(i=0; i<n;i++)
{
scanf("%s%f", name[i],&points[i]);
}
}
void printdata(char name[15][15],float points[15],int n)
{
int i;
for(i=0; i<n;i++)
{
printf("%s\t", name[i]);
printf("%.f\n", points[i]);
}
}

How to read a bidimensional array inside a function?

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++)

Returning Multiple variables between functions to in C

This is a simple enough problem I'm trying to figure out how to pass the int variables back to the other functions from the input function so it can be used in the stuff function to do the math then it returns the added variable and then passes all three to the output function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int input(int first, int second, int third);
int stuff(int first, int second, int third, int added);
int output(int first, int second, int third, int added);
int main(){
int first,second,third;
int added;
//sub functions
input(first,second,third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
int input(int first, int second, int third){
printf("Enter an interger for #1:");
scanf("%d",&first);
printf("Enter an interger for #2:");
scanf("%d",&second);
printf("Enter and interger for #3:");
scanf("%d",&third);
return first,second,third;
}
int stuff(int first, int second, int third, int added){
added = first + second + third;
return added;
}
int output(int first, int second, int third, int added){
printf("Integer 1 = %d\n",first);
printf("Integer 2 = %d\n",second);
printf("Integer 3 = %d\n",third);
printf("Integer 1,2,3 added together = %d\n",added);
}
Either use a struct
struct Foo {
int first, second, third;
}
struct Foo input() {
struct Foo foo;
printf("Enter an interger for #1:");
scanf("%d",&foo.first);
printf("Enter an interger for #2:");
scanf("%d",&foo.second);
printf("Enter and interger for #3:");
scanf("%d",&foo.third);
return foo;
}
Or pass pointers:
void input(int* first, int* second, int* third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
int main(){
int first,second,third;
int added;
//sub functions
input(&first,&second,&third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
You can use pointers and pass the address of those variables into each function. Every time you use it, you can dereference each variable in the functions.
You could just use pointers. I.e., change the function declaration to:
int input(int *first, int *second, int *third);
In the function itself, you use:
int input(int *first, int *second, int *third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
And when you call it, use:
input(&first,&second,&third);
You could do the following:
void input(int *a, int *b, int *c);
int main()
{
int first, second, third;
/* ... */
input(&first, &second, &third);
/* ... */
}
void input(int *a, int *b, int *c)
{
printf("Enter an interger for #1:");
scanf("%d", a);
printf("Enter an interger for #2:");
scanf("%d", b);
printf("Enter and interger for #3:");
scanf("%d", c);
}

Resources