for some reason I am unable to print out "* " in my program. I want to print out * if the condition is met. the condition is if the rainfall that day is greater than the average.
under the mean column, i am getting weird symbols. i tried debugging to decimals and get -112 for ascii. i dont understand but i tried researching!
I am new to C so please be understanding. Just learned like 2 days ago!!.
Here is my code :
//Assignment one 9/20/2018
//Anthony Lomaistro and David Luong
//luongd5#student.lasalle.edu
//lomaistroa1#student.lasalle.edu
#include <stdio.h>
main() {
int n = 0; //counters for the loops
int x = 0; // counter for the loops
int counter = 0; // counter whenever i need to keep track of increments
int days_input = 0; // how many days we are keeping track of
int number_of_days = 0;
double rainfall_input = 0;
double rainfall_amount = 0; // how much rainfall per day
double rainfall_average = 0; // average of rainfall
double rainfall_total = 0;
double rainfall_counter = 0; // count how many days it rained above the average
int correct = 0;
double rainfall_array[50];//array that contains the user input
char rainfall_condition[50]; // array that contains the *
double sum = 0;
double average = 0;
double percent_days = 0; //rainfall % above average
double valid = 0;
double valid2 = 0;
printf("Welcome to Lomaistro and Luong's Rainfall Program \n");
printf("How many days would you like to keep track of? Please enter a value between 1 and 50: #%d:", n + 1);
//printf(number_of_days);
while (valid == 0) {
scanf_s("%d", &number_of_days);
if ((number_of_days > 50) || (number_of_days <= 0)) { // come back to this, this doesnt do anytihng
printf("Invalid value, please enter in a day that is between 1 and 50 \n");
}
else {
valid = 1;
}
}
//getting the user to enter in the rainfall
for (x = 0; x < number_of_days; x = x + 1) {
valid2 = 0;
while (valid2 == 0) {
printf("Enter rainfall (in inches): ");
scanf_s("%lf", &rainfall_amount);
if ((rainfall_amount >= 0) && (rainfall_amount <= 10)) {
valid2 = 1;
rainfall_array[x] = rainfall_amount;
}
else
printf("Please enter in a valid rainfall amount between 1 and 10");
}
}
//computing average
for (n = 0; n < number_of_days; n = n + 1) {
sum += rainfall_array[n];
average = sum / number_of_days;
}
printf("Mean daily rainfall(in inches): %lf", average);
//seeing if the * should be the array or not
for (n = 0; n < number_of_days; n = n + 1) {
if (rainfall_array[n] > average) {
rainfall_condition[n] = "*";
rainfall_counter = rainfall_counter + 1;
}
else
rainfall_condition[n] = "empty";
}
// print out the thing
printf("\n Days \t Amount \t >Mean \n");
printf("==============================\n");
for (n = 0; n < number_of_days; n = n + 1) {
printf("%d \t %f \t %c \n", n + 1, rainfall_array[n], rainfall_condition[n]);
}
percent_days = rainfall_counter / number_of_days;
percent_days = percent_days * 100;
printf("Number of days that rained above average : %f \n", rainfall_counter);
printf("Percentage of days that rained above average: %f%% \n", percent_days);
system("pause");
}
rainfall_condition is an array of char, but you're putting a pointer to a string literal in there when you use "*". Use '*' for a character literal instead. To be more specific, this line:
rainfall_condition[n] = "*";
Should be:
rainfall_condition[n] = '*';
Turn some warnings on in your compiler; the first line (what you have now) isn't valid C code and you should be seeing a diagnostic message to that effect.
Edit: now that I've read more of the code, it appears you want either a * or an empty in that column? In that case you want to change the variable declaration to:
char *rainfall_condition[50]; // array that contains the *
And then change the print statement to:
printf("%d \t %f \t %s \n", n + 1, rainfall_array[n], rainfall_condition[n]);
Related
First I have to input N, N becomes the first number to be checked.
Input: 79
Output should be: 537.70.
int sum=0;
while(1)
{
scanf("%d", &n);
if(n>=10 && n<80)
{
break;
}
printf("New output:\n");
}
for(i=n;i<=1000;i++)
{
if(i%2==0 && i%6!=0 && i%17!=0)
{
sum+=i;
}
I didnt put (float)sum/N to get average because I'm doing something wrong with sum.
More input output:
Input: 10 Output: 505.21
Input: 44 Output: 521.18
As well as keeping a 'running sum', you also need to keep a count of how many numbers were used, so you can properly calculate the average:
#include <stdio.h>
int main(void)
{
int n;
printf("Enter start number: ");
scanf("%d", &n);
int sum = 0, count = 0;
for (int i = n; i <= 1000; ++i) {
if (!(i % 2) && (i % 6) && (i % 17)) {
sum += i;
++count;
}
}
printf("Average is: %.2f\n", (double)sum / (double)count);
return 0;
}
Input: 79
Output should be: 537.70.
Are you sure about this value? I get 538.70 - but I get the given values for the other test cases you cite.
I am working in an assignment for my intro to programming class.
Here is what my professor is asking:
Change the program without adding another loop, so it determines if the grade entered is valid, and if not, display an error message and keep asking until a valid grade is entered. Change the program, so it counts how many grades scored A, B, C, D, and F. Do NOT use stacked if or if-else constructs. Do NOT use the else if format. Output a table of the count of how many there are for each letter grade.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
/*Ask the user to enter 10 test scores.
The pass mark is 70%.
Calculate the average score, and count how many
of the students have passed the test.
*/
#define SCORES_COUNT 10
#define BAD_SCORE 60
#define PASSING_SCORE 70
#define GOOD_SCORE 80
#define EXCELLENT_SCORE 90
int main()
{
int scores[SCORES_COUNT];
int i, sum = 0, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
for (i = 0; i < SCORES_COUNT; i++)
{
printf("Enter score for student %i: ", i + 1);
scanf("%i", &scores[i]);
if (scores[i] > 100 || scores[i] < 0)
{
printf("\n\tERROR: Invalid number! Enter a score above zero and below one hundred!\n\n");
printf("Enter score for student %i: ", i + 1);
scanf("%i", &scores[i]);
}
}
for (i = 0; i < SCORES_COUNT; i++)
{
if (scores[i] >= EXCELLENT_SCORE)
{
sum++;
}
if (scores[i] >= GOOD_SCORE && scores[i] < EXCELLENT_SCORE)
{
sum1++;
}
if (scores[i] >= PASSING_SCORE && scores[i] < GOOD_SCORE)
{
sum2++;
}
if (scores[i] >= BAD_SCORE && scores[i] < PASSING_SCORE)
{
sum3++;
}
if (scores[i] < BAD_SCORE)
{
sum4++;
}
}
printf("\nGrades Distribution\n");
printf("\nA: %i", sum);
printf("\nB: %i", sum1);
printf("\nC: %i", sum2);
printf("\nD: %i", sum3);
printf("\nF: %i\n", sum4);
system("pause");
return 0;
}
The program works fine, but my teacher told me to not use stacked if or if-else constructs. And since I am a beginner that is the only way I know how to do it.
Can somebody please tell me how can I change all the if constructs, or how can I have the same result without using any if constructs?
I want to calculate the wages with struct and arrays but it is not working. You will understand the exercise better when you see the code. For some reason if in Wage and Days_Worked I enter a number(100 and 2)
Wagered[i].Gross_Wage = Wagered[i].Wage * Wagered[i].Days_Worked; Won't give me 200 but something else. Generally the programm won't work and I am trying to find the reason.
#include <stdio.h>
struct User
{
char First_Name[25];
char Last_Name[25];
int Wage, Days_Worked;
int Tax;
int Wage_Booking;
int Net_Wage;
int Gross_Wage;
};
int main()
{
int i;
int Wage_Summary = 0;
struct User Wagered[1];
for(i = 0; i < 1; i++)
{
/*printf("First Name: ");
scanf("%s", &Wagered[i].First_Name);
printf("\n");
printf("Last Name: ");
scanf("%s", &Wagered[i].Last_Name);
printf("\n");*/
printf("Wage: ");
scanf("%d", &Wagered[i].Wage);
printf("\n");
printf("Days He Worked: ");
scanf("%d", &Wagered[i].Days_Worked);
printf("\n");
Wagered[i].Gross_Wage = Wagered[i].Wage * Wagered[i].Days_Worked;
Wagered[i].Wage_Booking = Wagered[i].Gross_Wage * 0.2;
Wagered[i].Tax = (Wagered[i].Gross_Wage - Wagered[i].Wage_Booking) * 0.05;
Wagered[i].Net_Wage = Wagered[i].Gross_Wage - Wagered[i].Wage_Booking - Wagered[i].Tax;
Wage_Summary += Wagered[i].Net_Wage;
}
printf("The Summary of the Gross Wages is: %d\n", Wagered[i].Gross_Wage);
return 0;
}
This statement :
printf("The Summary of the Gross Wages is: %d\n", Wagered[i].Gross_Wage);
is outside your for loop. Therefore it will be executed when i has value 1, thus accessing Wagered[1].Gross_Wage, which does not exist Wagered[1] is out of your array's bounds. Move it inside your for loop, like this :
for(i = 0; i < 1; i++)
{
.
.
.
Wagered[i].Gross_Wage = Wagered[i].Wage * Wagered[i].Days_Worked;
Wagered[i].Wage_Booking = Wagered[i].Gross_Wage * 0.2;
Wagered[i].Tax = (Wagered[i].Gross_Wage - Wagered[i].Wage_Booking) * 0.05;
Wagered[i].Net_Wage = Wagered[i].Gross_Wage - Wagered[i].Wage_Booking - Wagered[i].Tax;
Wage_Summary += Wagered[i].Net_Wage;
printf("The Summary of the Gross Wages is: %d\n", Wagered[i].Gross_Wage);
}
and you will see the correct result printed.
I am completely new to programming and I'm currently taking a Intro to programming course. I need to adjust the below code to allow for an unspecified number of positive integers. I've looked this up and it seems to not take the average correctly. Please help. Thank you.
#include <stdio.h>
int main ()
{
/* variable definition: */
int count, value, sum;
double avg;
/* Initialize variables */
count = 0;
sum = 0;
avg = 0.0;
// Loop through to input values
while (count < 20)
{
printf("Enter a positive Integer\n");
scanf("%d", &value);
if (value >= 0) {
sum = sum + value;
count = count + 1;
}
else {
printf("Value must be positive\n");
}
}
// Calculate avg. Need to type cast since two integers will yield an
// integer
avg = (double) sum/count;
printf("average is %lf\n ", avg );
return 0;
}
You can use infinite loop and check for negative value and the return result of scanf as conditions to break.
Sample code looks like:
for(;;)
{
printf("Enter a positive Integer\n");
if(scanf("%d", &value) == 1 )
{
if (value >= 0) {
sum = sum + value;
count = count + 1;
}
else {
printf("Value must be positive\n");
break;
}
}
else
{
break;
}
}
Also, your initialization code is ok, but can be done cleaner this way (there is no need to separate between declaration and initialization - you can group them into one line):
int count = 0, value = 0, sum = 0;
double avg = 0;
I am really new to coding and I need to create a palindrome test that tests numbers up to a given limit. I understand the algorithm to test whether or not a number is a palindrome. However I'm having trouble looping the code.
The output should look like this:
if the limit is 1000:
limit | # of palindromes | sum of reciprocals
100 18 3.086147 (1/10 of the limit)
200 28 3.157490 (2/10 of the limit)
All the way up to the limit given by the user.
I have started the code however my code loops infinitely. Can you please tell me what I am doing wrong? Here is my code.
#include <stdio.h>
#include <math.h>
int main(void) {
double num;
int upperLimit = 0; //Limit of the program.
int numPalindromes = 0;
double sum = 0;
int tempLim;
int i = 1;
printf("Enter the limit of the program:"); //Asks for the limit you want the program to go to
scanf("%d", &upperLimit);
// We now need to use an algorithm to test whether or not the number is a palindrome.
while(num <= upperLimit) {
int temp;
int rev = 0;
temp = num;
while(temp != 0 ){
rev = rev * 10;
rev = rev + temp%10; // we need to add the remainder when the number is divided by 10 to the reverse.
temp = temp/10;
}
/*A number is a palindrome if its reverse is equal to itself. Now we must add its reciprocal to the sum and increase the amount of palindromes by 1 if the number is a palindrome */
if(num == rev) {
sum = sum + (1/num);
numPalindromes = numPalindromes + 1;
}
while (num <= upperLimit) {
tempLim = upperLimit * (i/10);
if (num == tempLim) {
printf(" %d %d %lf\n", tempLim, numPalindromes, sum);
}
i++;
}
num++;
}
}
You have such a snippet:
while (num <= upperLimit) {
tempLim = upperLimit * (i/10);
if (num == tempLim) {
printf(" %d %d %lf\n", tempLim, numPalindromes, sum);
}
i++;
}
However, neither num nor upperLimit is modified inside the loop. Thus it loops forever.
while (num <= upperLimit) {
tempLim = upperLimit * (i/10);
if (num == tempLim) {
printf(" %d %d %lf\n", tempLim, numPalindromes, sum);
}
i++;
}
num is not changing, upperlimit is not changing. So it runs infinitely.