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");
}
}
}
Related
This algorithm is a linear search algorithm that finds the desired value. But when I compile this code, gives the segmentation fault in if(dizi[i]==aranan) this line. How can I solve this ?
#include <stdio.h>
int N,i,aranan;
int ArrayBastir(int *dizi,int N)
{
printf("My numbers\n");
for(int i =0; i<N;i++)
{
printf("%d\n", dizi[i]);
}
}
int findValue()
{
printf("The value you want to search");
scanf("%d",&aranan);
}
int Output(int *dizi,int N,int aranan)
{
for(int i =0; i<N;i++)
{
if(dizi[i]==aranan)
{
printf("%d number %d. found in queue \n", aranan,i+1);
}
}
}
int main() {
int N;
int aranan;
printf("Please enter how many numbers you want to enter");
scanf("%d", &N);
int dizi[N];
for(int i =0; i<N;i++)
{
scanf("%d", &dizi[i]);
}
ArrayBastir( dizi, N);
findValue(aranan);
Output(*dizi,N,aranan);
return 1;
}
Linear Search algorithm
You have two objects defined as:
int aranan;
Once is defined at file scope (a global), and one is defined within the scope of main.
When findValue(aranan) is called, a copy of the uninitialized value of aranan within the scope of main is passed to findValue. findValue is lacking a prototype, having not declared its arguments, so this is ignored.
findValue scans a value into the file scope aranan, but when Output(*dizi, N, aranan) is called it uses the value of aranan defined within main. aranan within main was never initialized, and thus this causes Output to search for an indeterminate value.
Additionally, *dizi is an int, when Output expects an int * as its first argument.
ArrayBastir and Output are also defined as each returning an int, which they do not do.
You should not ignore the return value of scanf, as it indicates the number of successful conversions, or a negative value on failure (EOF). In a program this small, you can get away with writing a simple wrapper function for reading integers, that just exits the program if the user enters something invalid.
main returning nonzero generally indicates your program failed. main is special - it is the only non-void function where you can omit a return statement. If main reaches the end of execution without an explicit return, it is treated as though it returned 0.
The issues above can be mitigated by avoiding the use of global variables, and by turning up your compilers warning level to catch obvious type mismatches.
For GCC or Clang, use -Wall -Wextra, and possibly -Werror.
For MSVC, use /Wall, and possibly /Wx.
Minimally refactored:
#include <stdio.h>
#include <stdlib.h>
int get_int(void)
{
int x;
if (1 != scanf("%d", &x)) {
fprintf(stderr, "Invalid input.\n");
exit(EXIT_FAILURE);
}
return x;
}
void ArrayBastir(int *dizi, int N)
{
printf("My numbers\n");
for (int i = 0; i < N; i++) {
printf("%d\n", dizi[i]);
}
}
void Output(int *dizi, int N, int aranan)
{
for (int i = 0; i < N; i++) {
if (dizi[i] == aranan) {
printf("Found <%d> at position %d.\n", aranan, i);
}
}
}
int main(void)
{
printf("Please enter how many numbers you want to enter: ");
int N = get_int();
int dizi[N];
for (int i = 0; i < N; i++) {
dizi[i] = get_int();
}
ArrayBastir(dizi, N);
printf("Enter the value you want to search for: ");
int aranan = get_int();
Output(dizi, N, aranan);
}
#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
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]);
}
}
The code need to send to the function an int must to send just int and show the changes after the function. so I tried this:
int main(void)
{
int number = 0;
printf("Please enter some number: \n");
scanf("%d", &number);
printf("the number that you entered: %d\n", number);
inc(number);
printf("After the 'inc' function, your number is: %d\n", number);
system("PAUSE");
return 0;
}
/*
*/
void inc(int x)
{
int* px = &x;
*px= *px+1;
}
it's prints just the same number and doesn't change at all. help?
you must pass the parameter as a pointer
void inc(int *px)
{
*px= *px+1;
}
in your code you are just modifying the functions local copy of x
A hacktacularly terrible hackaround:
inc((int)&number);
// ...
void inc(int n)
{
++*(int*)n;
}
Don't do this. It won't work on all but certain archaic architectures.
Background: I have an exercise which asks me to create a function which compares 2 int arrays using the backtracking technique. The function should return 0 if the arrays are different and 1 if they are the same. The size of the arrays, the method of filling them and the output of the program are not specified in the question so I took the liberty of working them out my own way.
By using a for I made a simple fill function which fills out the two arrays in a simple way so if the user inputs s the result should be
A[0]=B[0]=0
A[1]=B[1]=1
...
A[50]=B[50]=50
and if he inputs d it should be the same but
B[i]=A[i]+1
The problem:
Instead of A[0]=0 it ends up being A[0]=50 (and A[0]=51 in the d case) which makes the whole function return 0 in every case. I have tried numerous things, but I can't get it to work properly
Here's the code:
#include <stdio.h>
#include <stdlib.h>
void fill(int a, int A[],int B[])
{
int i,j;
if (a)
{
for(i=0;i<=50;i++)
{
A[i]=i;
B[i]=A[i];
}
}
else
{
for(i=0;i<=50;i++)
{
A[i]=i;
B[i]=i+1;
}
}
A[0]=0;
for (j=0;j<=50;j++)
printf("\nka %d %d %d",j, A[j],B[j]); //the purpose of this is to check the state of the two arrays after filling them, it's how I spotted the problem, it will be deleted in the final form
}
int compare(int i, int A[],int B[])
{
int a,b;
a=A[i];
b=B[i];
printf("j %d %d\n", a,b);
if (B[i+1]!='\0')
{
if (A[i]==B[i])
{
compare (i+1,A,B);
}
else
{
return 0;
}
}
else
return 1;
}
int main()
{
int A[50], B[50], i=0;
char s;
printf("Do you want the arrays to be the same or different?\n");
printf("Input 's' or 'd': ");
scanf("%c", &s);
switch(s)
{
case 's':
case 'S':
fill(1,A,B);
break;
case 'd':
case 'D':
fill(0,A,B);
break;
default:
printf("Sorry incorrect input, please input 's' or 'd'");
return 0;
break;
}
if (compare(i,A,B))
printf("They are the same");
else
printf("They are different");
return 0;
}
You need to correct your both the functions fill and compare. In function fill you are accessing arrays out of bounds which will invoke undefined behavior. Loop should be iterated from 0 to 50 (excluding 50)
for(i=0;i<50;i++) { ... }
The function compare should be like
// Pass the size of array to the function.
int compare(int i, int A[],int B[], int n)
{
if (i != n)
{
if (A[i]==B[i])
{
return compare (i+1, A, B, n);
}
else
{
return 0;
}
}
else
return 1;
}
The issue is in the for loop. It is looping for 51 elements.
for (i=0;i<=50;i++)
It should loop for 50 elements
for (i=0;i<50;i++)
Similarly for the loop for j, in the fill function.
There is also an issue in the compare function. The exit check is
if (B[i+1]!='\0')
This means that the input array must have the last element as '\0'. Which is not happening in your input array.
You have to either pass strings to this function, or modify this function to take care of the general array case.