#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
Related
the following is given
#include <stdio.h>
void func2 (int num, int *result);
int main()
{
int num, result;
printf("Enter a number: \n");
scanf("%d", &num);
func2(num, &result);
printf("func2(): %d\n", result);
return 0;
}
void func2(int num, int *result)
{
//key in code
}
in the void func2 i wrote
int i=0;
result=&i;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
but the programming is not returning the value of variable i properly. what's wrong with my variable assignment?
expected output:
Enter a number:
24 (user enter)
func2(): 20
actual output:
Enter a number:
24 (user enter)
func2(): 32767
You need to assign indirectly through result, not set result to the address of another variable.
int i=0;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
*result = i;
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
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]);
}
}
I'm trying to learn C and I want to make a program that compares the number I type with the numbers in my array. The only problem is that It doesn't actually do that. Even if I type a number that is from that array it shows that the number is not from that array.
#include <stdio.h>
void getMark(int findMark, double crswk1[]);
void changePartMark(double crswk1[], int findMark);
int main()
{
int findMark;
double crswk1[10]={67, 77, 80, 40};
getMark(findMark, crswk1);
changePartMark(crswk1, findMark);
}
void getMark(int findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
void changePartMark(double crswk1[], int findMark)
{
int i;
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
The number you're reading in is never getting back to findMark in your main function.
void getMark(int findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
This function is saving a value in the local parameter findMark. Because all parameters in C are passed by value, changes to this local variable are not reflected in the caller, so findMark in main never changes.
You need to change this function to take the address of an `int
void getMark(int *findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", findMark);
}
Then you call this function from main like this:
getMark(&findMark, crswk1);
By passing in the address of findMark, the function can write to that address.
Also, your changePartMark function doesn't search through the entire array. It only looks at index i. But even that is a problem because you never set i.
You need to loop through the array to check your value against each element in the array.
int i;
for (i=0; i<4 i++) {
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
Two main issues:
First, the number you enter is never passed back. When you write
void getMark(int findMark, double crswk1[]) {
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
then you read in the value in a local copy findMark, not in the one used by the caller. BTW: crswk1 is not used; So I'd suggest to write
int getMark() {
int findMark = 0;
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
return findMark;
}
Second, your void changePartMark(double crswk1[], int findMark) lacks a loop, and i is not initialized. Code could look like the following:
void changePartMark(double crswk1[], int findMark)
{
for (int i=0; i<4; i++) {
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
}
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