Since I am a beginner have been practicing passing arrays as parameters to functions. Could someone explain why this program I wrote works fine on codeblox, but doesn't work in Fedora terminal (on Fedora it doesn't work for any number of scores). I'm using the GCC compiler.
#include<stdio.h>
int num,scores[]={},count,max,sum;
void input(int scores[]);
int findmax(int scores[]);
int findsum(int scores[]);
void display(int max,int sum);
int main()
{
printf("Enter number of scores\n");
scanf("%d",&num);
input(scores);
max=findmax(scores);
sum=findsum(scores);
display(max,sum);
return 0;
}
void input(int scores[])
{
for(count=0; count<num; count ++)
{
printf("Enter score #%d:\n",count);
scanf("%d",&scores[count]);
}
}
int findmax(int scores[])
{
for(count=0; count<num ; count ++)
{
if(scores[count]>max)
{
max=scores[count];
}
}
return max;
}
int findsum(int scores[])
{ sum=0;
for(count=0; count<num ; count++ )
{
sum=sum+scores[count];
}
return sum;
}
void display(int max,int sum)
{
printf("The max score is :%d\nThe total score is:%d\n",max,sum);
}
You should avoid using global variables unless they are absolutely required. None are required here. All values should be declared local to main and should be passed to each function as a parameter, as required.
You must validate ALL input by, at minimum, checking the return of scanf (or whatever input function you are using). If a user simply pressed Enter (generating a '\n') or enters words instead of numbers causing the conversion to fail, you are dead in the water and may well be watching an infinite loop scroll by until you kill the process. (a cat might step on the keyboard, etc.)
When using the scanf family of functions, if a failure occurs, you must account for any characters left in the input buffer (e.g. stdin with scanf). If scanf is called within a loop, you risk an infinite loop where the conversion fails, leaving the same characters in the input buffer only to be read again, and fail, over-and-over again.
If you do not validate your input, you can have no confidence that you are processing a valid input from that point forward, and may in fact be well off into Undefined Behavior. The scanf family of functions returns the number of valid conversions based on the number of format-specifiers contained within the format-string. So with "%d", you have one format-specifier (e.g. %d), so upon successful conversion the return will be 1 (you should also check for EOF if the user manually cancels input with Ctrl + d, or Ctrl + z on windoze -- which is left to you)
Putting those pieces together, you could do something like the following:
#include <stdio.h>
#include <limits.h> /* for INT_MIN */
#define MAX 512
void input (int *scores, int num);
int findmax (int *scores, int num);
int findsum (int *scores, int num);
void display (int max, int sum);
int main (void)
{
int num, scores[MAX] = { 0 }, max, sum;
printf ("Enter number of scores: ");
if (scanf ("%d", &num) != 1) { /* VALIDATE ALL input */
fprintf (stderr, "error: invalid integer input.\n");
return 1;
}
input (scores, num);
max = findmax (scores, num);
sum = findsum (scores, num);
display (max, sum);
return 0;
}
void input (int *scores, int num)
{
int count = 0;
while (count < MAX && count < num) { /* always protect array bounds */
printf ("Enter score [%d]: ", count);
while (scanf ("%d", &scores[count]) != 1) { /* VALIDATE input */
fprintf (stderr, "error: invalid input, try again.\n");
/* remove invalid chars from input buffer */
for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}
goto badval;
}
count++;
badval:;
}
}
int findmax (int *scores, int num)
{
int max = INT_MIN, count;
for (count = 0; count < num; count++)
if (scores[count] > max)
max = scores[count];
return max;
}
int findsum (int *scores, int num)
{
int sum = 0, count;
for (count = 0; count < num; count++)
sum = sum + scores[count];
return sum;
}
void display (int max, int sum)
{
printf ("\nThe max score is :%d\nThe total score is:%d\n", max, sum);
}
Example Use/Output
$ ./bin/maxminsum
Enter number of scores: 5
Enter score [0]: 9
Enter score [1]: 3
Enter score [2]: 7
Enter score [3]: 8
Enter score [4]: 2
The max score is :9
The total score is:29
With Error Correction
$ ./bin/maxminsum
Enter number of scores: 5
Enter score [0]: 9
Enter score [1]: 3
Enter score [2]: foo
error: invalid input, try again.
Enter score [2]: 7
Enter score [3]: 8
Enter score [4]: bar
error: invalid input, try again.
Enter score [4]: 2
The max score is :9
The total score is:29
Leave a comment if you have further questions.
Related
I've searched for solutions to that problem and couldn't find any that match mine.
I wrote a program that gets two arrays of integers and return the scalar product between them. It works fine when I'm submitting the input manually, but when I try to read the input from a text file, I encounter that Segmentation fault.
Edit: I'm talking about stdin redirection
I would be grateful for some help.
The code is:
#include <stdio.h>
#define MAXLIMIT 100
int scalar_product(int[], int[], int);
void set_array(int[]);
int main(){
int arr1[MAXLIMIT], arr2[MAXLIMIT];
int size, result;
set_array(arr1);
set_array(arr2);
printf("Enter the vectors' dimension: ");
scanf("%d", &size);
result = scalar_product(arr1, arr2, size);
printf("The scalar product is: %d \n", result);
return 0;
}
void set_array(int a[]){
int i;
printf("Please enter a vector with up to %d elements: \n", MAXLIMIT);
for (i = 0; i < MAXLIMIT - 1 && (scanf("%d", &a[i]) != EOF); i++);
}
int scalar_product(int a1[], int a2[], int size){
int product = 0, i;
for (i = 0; i < size; i++){
product += a1[i] * a2[i];
}
return product;
}
and the text file contains:
1 -2 3 -4
6 7 1 -2
4
HEre
void set_array(int a[]) {
int i;
printf("Please enter a vector with up to %d elements: \n", MAXLIMIT);
for (i = 0; i < MAXLIMIT - 1 && (scanf("%d", &a[i]) != EOF); i++);
}
When reading from the console you will never hit EOF (unless you enter ctrl-D which I guess you didnt) so your set_array loops just keep going, reading from the file. You read all the data in the first set_array and read nothing in the second one because you have finished the input file
the actualk failure was that you ran off the end of the file, so the scanf of size failed and you were trying to read a random sized array in the function scalar_product.
Test the return from scanf always
What you need to do is put a count in the file before the first array so you know how many items to read into arr1 and I suggest a count before the second lot too.
ie
void set_array(int a[]) {
int i;
int count = 0;
printf("Please enter how many elements you want to enter, max = %d \n", MAXLIMIT);
scanf("%d", &count);
if(count > MAXLIMIT) count = MAXLIMIT;
for (i = 0; i < count && (scanf("%d", &a[i]) != EOF); i++);
}
I am trying to understand why I am having a zero added on to one of my variables userNumberInput. I have been trying to figure out why this is happening for the a bit without a good solution or explanation. It is only happening in the output file, and does not appear to be happening as a result of the variable being set to 0.
For example if you enter 90 the console shows 90, but the output file shows 900.
#include <stdio.h>
#include <stdlib.h>
void Fibonacci (int userInputNumber);
void UserInput (int* userInputNumber);
FILE *fpOut;
int main(int argc, const char * argv[]) {
//Creating global variable for userInput to be passed from function to function
int userInputNumber = 0;
//Opening file for writing output to
if (!(fpOut = fopen("csis.txt", "w"))) {
printf("csis.text could not be opened for output");
exit(1);
}
//Calling two functions and passing appropriate vaiables to each
UserInput(&userInputNumber);
Fibonacci(userInputNumber);
fclose(fpOut);
return 0;
}
void UserInput (int* userInputNumber) {
//Asks the user for a number for the generator
printf("Enter a number into the Fibonacci Generator: ");
scanf("%d", &*userInputNumber);
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d", *userInputNumber);
//While the user continues to enter a negative number it forces the user to enter a new number
while (*userInputNumber < 0) {
printf("Invalid user input, enter a positive number\n\n");
fprintf(fpOut,"Invalid user input, enter a positive number\n\n");
printf("Enter a number into the Fibonacci Generator: ");
scanf("%d", &*userInputNumber);
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d", *userInputNumber);
}
}
void Fibonacci (int userInputNumber) {
//Defines variables used in this function
int firstNumber, secondNumber, i, seriesNumber, length;
firstNumber = 0;
secondNumber = 1;
seriesNumber = 0;
i = 0;
length = 0;
//For loop that adds the two previous number in the series and prints it and then updates the previous numbers
for (i = 0; seriesNumber < userInputNumber; i++) {
if (i <= 1) {
seriesNumber = i;
length++;
} else {
seriesNumber = firstNumber + secondNumber;
firstNumber = secondNumber;
secondNumber = seriesNumber;
length++;
}
//Statement to break the loop when the next number in the series goes above the user input
if (seriesNumber > userInputNumber) {
length -= 1;
break;
}
//Printing the sequence
printf("%d\n",seriesNumber);
fprintf(fpOut,"%d\n",seriesNumber);
}
//Printing the length of the sequence
printf("Length of the sequence: %d", length);
fprintf(fpOut,"Length of the sequence: %d", length);
}
You have to print a newline after printing the value entered, or 0, which is the first term of the sequence, seems to be appended to the input.
Try changing both of the two
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d", *userInputNumber);
to
fprintf(fpOut,"Enter a number into the Fibonacci Generator: %d\n", *userInputNumber);
I have asked for the user to enter in several values to calculate an average, however I'd like to also calculate a gradient which uses the inputted values. How do I name these values so I can use them again? Thank you.
Here is what I have thus far:
#include <stdio.h>
int main () {
int n, i;
float num[1000], total=0, mean;
printf("Enter the amount of x-values:");
scanf("%d", &n);
while (n <= 0 || n > 1000) {
printf("Print error. The number should in range of 0 to 1000.\n");
printf("Please try to enter the amount again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Input x-value:", i+1);
scanf("%f", &num[i]);
total += num[i];
}
mean=total/n;
printf("The mean of all the x-values entered is %.2f to 2 decimal places", mean);
{
float num[1000], total=0, mean;
printf("Enter the amount of y-values:");
scanf("%d", &n);
while (n <= 0 || n > 1000) {
printf("Print error. The number should in range of 0 to 1000.\n");
printf("Please try to enter the amount again: ");
scanf("%d",&n);
}
for (i = 0; i < n; ++i) {
printf("%d. Input y-value:", i+1);
scanf("%f", &num[i]);
total += num[i];
}
mean = total / n;
printf("The mean of all the y-values entered is %.2f to 2 decimal places", mean);
return 0;
}
}
Naming the variable is really up to you, but `int gradient[NUM_ELEMENTS]; seems appropriate. It is an array, which also seems appropriate if it's purpose is to assist in finding the gradient from a series of numbers.
Steps could be:
1) use printf to ask user for values, specify spaces or commas between values. You can also specify a limit of values. Example printf("enter 5 numbers separated by commas\n:");
2) use scanf or similar to read values from standard input (the terminal) into the array: scanf("%d,%d,%d,%d,%d", &gradient[0], &gradient[1], &gradient[2], &gradient[3], &gradient[4]);
3) use the array an a function that will compute the gradient.
Simple example:
(where gradient computation, error checking, bounds checking etc. is all left to you)
int get_gradient(int a[5]);
int main(void) {
int gradient[5];
int iGradient=0;
printf("enter 5 numbers separated by commas");
scanf("%d,%d,%d,%d,%d", &gradient[0], &gradient[1], &gradient[2], &gradient[3], &gradient[4]);
iGradient = get_gradient(gradient);
return 0;
}
int get_gradient(int a[5])
{
return [do computation here using array a];
}
Edit:
The above example works only if you know the number of elements at compile time. It uses an int array that has been created on the stack. If you do not know how big of an array you will need until run-time, for example if user input determines the size, then the array size needs to be determined at run-time. One options is to create the variable needed on the heap. (read about stack and heap here) The following uses some techniques different from the simpler version above to get user input, including a function I called get_int(), which uses scanf() in conjunction with strtol(). Here's the example:
int main(void) {
char input[80]={0};
char **dummy={0};
long *gradient = {0};
int iGradient=0;
int arraysize;
int i;
char* fmt = "%[^\n]%*c";
printf("how many numbers will be entered?");
scanf(fmt, input);
arraysize = strtol(input, dummy, 10);
gradient = calloc(arraysize, sizeof(long));
if(gradient)
{
for(i=0;i<arraysize;i++)
{
gradient[i] = get_int();
}
iGradient = get_gradient(gradient, arraysize);
//free gradient when done getting result
free(gradient);
}
return 0;
}
int get_gradient(int *a, int num)
{
int grad = 0, i;
//do something here to compute gradient
return grad;
}
long get_int(void)
{
char input[80]={0};
char **dummy={0};
char* fmt = "%[^\n]%*c";
printf("Enter integer number and hit return:\n");
scanf(fmt, input);
return strtol(input, dummy, 10);
}
Well it is a problem about finding the biggest and smallest number in a group of numbers, but we do not know how many numbers the user wants-
So far this is what i have done:
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int i;
int maxi=0;
int minim=0;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf ("%d", &num);
while (num>0)
{
printf ("\nEnter number:");
scanf ("%d", &i);
if (num>i)
minim=i++;
else
if (i>num)
max=i++;
cont++;
}
printf ("\nBiggest number is es: %d", maxi);
printf ("\nSmallest number is: %d", minim);
getch();
return 0;
}
I did my program to ask how many numbers the user will want to put and i made the program to read them, BUT when it reads the biggest or/and smallest numbers it will sometimes changes biggest with small and it will not read negative numbers.
How do i do to make my program better?
You're comparing against the wrong values.
do
{
printf("Enter a number.\n");
scanf("%i", &input);
if min > input
min = input
if max < input
max = input
} while (input > 0);
#include <stdio.h>
#include <conio.h>
#include <limits.h>
int main(){
int num;
int i;
int maxi=0;
int minim=INT_MAX;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf("%d", &num);
if(num > 0){
while (num>0){
printf ("\nEnter number:");
if(scanf("%d", &i) == 1 && !(i<0)){
if(minim > i)
minim = i;
if (maxi < i)
maxi = i;
++cont;
--num;
} else {
//fprintf(stderr, "redo input!\n")
;
}
scanf("%*[^\n]%*c");
}
printf ("\nBiggest number is : %d", maxi);
printf ("\nSmallest number is : %d\n", minim);
}
getch();
return 0;
}
You should initialize mini to the largest possible int, i.e. INT_MAX and maxi to the smallest possible int, i.e., INT_MIN. This way, even if the first number is negative, it will be considered for maxi, and if the first number is positive it will still be considered for mini. The constants INT_MAX and INT_MIN are included in <climits> or <limits.h>.
Also, you are comparing the current entered number with num, which is the counter of numbers entered by user, not one of the values he wants to compare. A better modified code would be :
#include<limits.h>
#include<stdio.h>
int main()
{
int num;
int maxi=INT_MIN; //initialize max value
int mini=INT_MAX; //initialize min value
int temp;
scanf("%d", &num); //take in number of numbers
while(num--) //loop "num" times, num decrements once each iteration of loop
{
scanf("%d", &temp); //Take in new number
if(temp>maxi) //see if it is new maximum
maxi=temp; //set to new maximum
if(temp<mini) //see if new minimum
mini=temp; //set to new minimum
}
printf("\nMaxi is:\t%d\nMini is:\t%d\n", maxi, mini); //print answer
return 0;
}
I am very new to C. I am using A modern Approach to C programming by King 2nd Edition.
I am stuck on chapter 6. Question 1: Write a program that finds the largest in a series of numbers entered by the user. The program must prompt the user to enter the numbers one by one. When the user enters 0 or a negative number, the program must display the largest non negative number entered.
So far I have:
#include <stdio.h>
int main(void)
{
float a, max, b;
for (a == max; a != 0; a++) {
printf("Enter number:");
scanf("%f", &a);
}
printf("Largest non negative number: %f", max);
return 0;
}
I do not understand the last part of the question, which is how to see which non-negative number is the greatest at the end of user input of the loop.
max = a > a ???
Thanks for your help!
So you want to update max if a is greater than it each iteration thru the loop, like so:
#include <stdio.h>
int main(void)
{
float max = 0, a;
do{
printf("Enter number:");
/* the space in front of the %f causes scanf to skip
* any whitespace. We check the return value to see
* whether something was *actually* read before we
* continue.
*/
if(scanf(" %f", &a) == 1) {
if(a > max){
max = a;
}
}
/* We could have combined the two if's above like this */
/* if((scanf(" %f", &a) == 1) && (a > max)) {
* max = a;
* }
*/
}
while(a > 0);
printf("Largest non negative number: %f", max);
return 0;
}
Then you simply print max at the end.
A do while loop is a better choice here because it needs to run at least once.
#include<stdio.h>
int main()
{
float enter_num,proc=0;
for(;;)
{
printf("Enter the number:");
scanf("%f",&enter_num);
if(enter_num == 0)
{
break;
}
if(enter_num < 0)
{
proc>enter_num;
proc=enter_num;
}
if(proc < enter_num)
{
proc = enter_num;
}
}
printf("Largest number from the above is:%.1f",proc);
return 0;
}