my function inside of c program does not work - c

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]);
}
}

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

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

General function Input array

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

Trying to pass an array to a function in c

I have been asked to write a lottery program in c++ not sure if i am using function correctly please help
//function prototypes
int myNumbers();
void displayNums();
#include <stdio.h>
#define NUMS 6
#define WIN 7
// function myNumbers takes input from user
int myNumbers(int numbers[]) //implememt function
{
int i;
int numbers;
int input[NUMS];
printf ("Please enter your lucky numbers\n");
for (i=0;i<NUMS;i++)//loop through array to take input
{
scanf("%d",&input[i]);
}//end for loop
return (numbers);
}//end function myNumbers
Each of your functions declares a new array, rather than using the array passed to it as an argument.
void displayNums(int print[])
{
int i;
int output;
int numbers[NUMS];
output = myNumbers(numbers);
printf("Your numbers are %d \n", output);
}
Note that nowhere is the print argument used, and you're using int numbers[NUMS] instead. Remove that declaration and use print. (Also please consider naming your argument something other than print; this name is confusing and does not accurately describe what the variable stores.)
you are not using arrays properly to communicate the numbers, see the function
int myNumbers(int userPick[]) //implememt function
{
int i;
int numbers;
int input[NUMS];
printf ("Please enter your lucky numbers\n");
for (i=0;i<NUMS;i++)//loop through array to take input
{
scanf("%d",&input[i]);
}//end for loop
numbers = *(input+i);
return (numbers);
}//end function myNumbers
it reads the number to a local array and returns *(input+i) which will be a random number since your read array is from input+0 to input+i-1. you should pass array or pointer to global array.
Even in case of display() function you are passing one array and using some other array inside display() function.
you should use a common array to communicate values. you can create a array in global scope and use it in all functions or create a array in main() and pass pointer to it to other functions and use the same array in other functions. learn how to pass arrays between functions and use arrays
Your functions looks erroneous:
int myNumbers(int userPick[]) // 1. userPick is not used anywhere
{
int i;
int numbers; // 2. initialize this variable to 1 first
int input[NUMS];
printf ("Please enter your lucky numbers\n");
for (i=0;i<NUMS;i++)
{
scanf("%d",&input[i]);
}//end for loop
numbers = *(input+i); // 3. is this suppose to be inside the loop ?
return (numbers);
}//end function myNumbers
And in
void displayNums(int print[])//implement function
{
int i;
int output;
int numbers[NUMS];
output = myNumbers(numbers);
printf("Your numbers are %d \n",output);
}//end function displayNums
You don't use print but create a new array numbers
Most of your code is erroneous. It won't compile. Here's some sample code. Work with it to implement the rest
#include <stdio.h>
#define NUMS 6
void displayNums(int nums[])
{
int i;
printf("Your numbers are:\n");
for(i = 0; i < NUMS; i++)
printf("%d ", nums[i]);
printf("\n");
}
void myNumbers(int nums[])
{
printf("Please Enter your lucky numbers\n");
int i;
for(i = 0; i < NUMS; i++)
scanf("%d", &nums[i]);
}
int main()
{
int numbers[6];
myNumbers(numbers);
displaynums(numbers);
//do the rest of the stuff here
return 0;
}
Going through a C tutorial might be helpful? Check out http://c.learncodethehardway.org/book/ for a nice intro to the language.

Resources