I need some help displaying a float array that's partially filled. Not sure what I'm doing wrong, but here's what I have.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#define SIZE 20
int main()
{
float score[SIZE];
float GPA, sum, avg;
int count, ctr, num_of_entries;
sum = 0;
for (count = 0; count < SIZE; count++)
{
printf("Enter a GPA. -1 to stop the data entry: ");
scanf("%f", &GPA);
if (GPA == -1)
break;
score[count] = GPA;
}
printf("Number of GPAs entered = %d", count);
num_of_entries = count;
printf("\n\nContent of the Array:\n=========================\n");
for (ctr = 0; ctr <= num_of_entries; ctr++);
{
printf("%.2f \n", score[ctr]);
}
_getch();
return 0;
}
I'm trying to display the entered GPA values as float variables with 2 decimal places. The printed result is a very large negative number. Any help would be greatly appreciated.
Related
If I input this number: 1234, I want the output to begin with 1 2 3 4 not 4 3 2 1. How do I do this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numbers, count=0, num;
printf("\nEnter numbers: ");
scanf("%d", &numbers);
while(numbers>0)
{
num = numbers%10;
numbers = numbers/10;
printf("%d", num);
}
printf("The total number of digits is: %d\n", num);
return 0;
}
One of the solutions is to use recursion.
#include <stdio.h>
#include <stdlib.h>
// prints reverse order and returns count of digits
int printrev(int numbers)
{
if (numbers <= 0)
return 1;
int num = numbers % 10;
int count = printrev(numbers / 10);
printf("%d", num);
return count + 1;
}
int main()
{
int numbers, count = 0, num;
printf("\nEnter numbers: ");
scanf("%d", &numbers);
count = printrev(numbers);
printf("\nThe total number of digits is: %d\n", count);
return 0;
}
This topic was already answered in this stackoverflow question. Also, an easier way is to create an array where you can save the digits inside of while loop and then print it backwards. But in this case it would need to declare an array a priori.
How about to display character by character in string order?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numbers = 0;
printf("Enter numbers: ");
scanf("%d", &numbers);
char szNumber[16] = "";
snprintf(szNumber, sizeof(szNumber), "%d", numbers);
int i = 0;
while (szNumber[i] != '\0')
{
printf("%c ", szNumber[i]);
++i;
}
return 0;
}
So I have some code that generates a set of random numbers. Afterwards, it asks the user if they would like to produce another set of random numbers. The problem I'm having is that I want the user to only input y for yes or n for no, and if they don't input those to options, tell them that is an invalid input and to try again. I cannot figure out how to do this. Here is the code:
// C program for generating a
// random number in a given range.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size:\n");
scanf("%d", &lower);
printf("\nMaximum number size:\n");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated:\n");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
scanf(" %c",&c);
printf("\n");
if (c=='n'){
exit(0);
}
do {
printf("Please enter valid input!\n");
scanf(" %c", &c);
if (c=='n'){
exit(0);
}
} while (c!='y'||c!='n');
} while(c=='y');
return 0;
}
Any help would be appreciated.
This answers your question, but on a side note, I usually like putting the conditional in the first question so if the user enters zero then the app will quit.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size: ");
scanf("%d", &lower);
printf("\nMaximum number size: ");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated: ");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
do {
printf("\t\t==> ");
scanf(" %c", &c);
c &= 0x5f; // convert to uppercase
if (c=='N') exit(0);
} while (c != 'Y');
} while (1);
return 0;
}
I have to create a recursive function that tells you the number of ways a number of cents can be made into change. (Using quarters, dimes nickels, and pennies).
So far, I have a recursive function that does that, however it counts the same combination more than once, so the number is too big. How do I remove the duplicate combinations?
Code:
#include <stdio.h>
//Prototypes
int coins(int);
int main(void){
//Declarations
int num;
//Get user input
printf("Enter an amount of change in cents: ");
scanf("%d", &num); //Change to fgets
//Call function
printf("There are %d ways to make change for %d cents.\n", (coins(num)), num);
}
int coins(int amt){
//Declarations
int ways=0;
//Base Case
if(amt == 0){
return 1;
}
//int ways=0; More efficient after base case.
if(amt >= 1){
ways+=coins(amt-1);
}
if(amt >= 5){
ways+=coins(amt-5);
}
if(amt >= 10){
ways+=coins(amt-10);
}
if(amt >= 25){
ways+=coins(amt-25);
}
return ways;
}
Example:
Input: 17 (cents)
Output: 80 ways
**Output should be 6
#include <stdio.h>
int coins(int, int);
int main(void){
int num;
printf("Enter an amount of change in cents: ");
scanf("%d", &num);
printf("There are %d ways to make change for %d cents.\n", coins(num, 0), num);
return 0;
}
int coins(int amt, int kind){
static int kinds[4] = {25, 10, 5, 1};
int ways=0, i, n;
if(kinds[kind] == 1)//always divisible
return 1;
n = amt / kinds[kind];
for(i = 0; i <= n; ++i)
ways+=coins(amt-kinds[kind]*i, kind + 1);
return ways;
}
I made a C program to check if a number is palindrome or not. I used the following code, but it shows numbers like 12321 as non palindrome. Can you please explain me the mistake in the program below?
#include <stdio.h>
int main()
{
int i, x, n, c, j;
int d=0;
printf ("enter total digits in number: ");
scanf ("%d", &i);
printf ("\nenter number: ");
scanf ("%d", &n);
j=n;
for (x=1; x<=i; x++)
{
c= j%10;
d=c*(10^(i-x))+d;
j=(j-c)/10;
}
if (d==n)
{
printf ("\npalindrome");
}
else
{
printf ("\nnon palindrome");
}
return 0;
}
^ is the xor operator.
In order to raise power, you need to include math.h and call pow
d = (c * pow(10, i - x)) + d;
this algorithm is as simple as human thinking, and it works
#include <stdio.h>
int main() {
int i=0,n,ok=1;
char buff[20];
printf("Enter an integer: ");
scanf("%d", &n); // i am ommiting error checking
n=sprintf(buff,"%d",n); //convert it to string, and getting the len in result
if(n<2) return 0;
i=n/2;
n--;
while(i && ok) {
i--;
//printf("%c == %c %s\n", buff[i],buff[n-i],(buff[i]==buff[n-i])?"true":"false");
ok &= (buff[i]==buff[n-i]);
}
printf("%s is %spalindrome\n",buff, ok?"":"not ");
return 0;
}
// Yet another way to check for palindrome.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int n, rn, tn;
printf("Enter an integer: ");
scanf("%d", &n);
// reverse the number by repeatedly extracting last digit, add to the
// previously computed partial reverse times 10, and keep dropping
// last digit by dividing by 10
for (rn = 0, tn = n; tn; tn /= 10) rn = rn * 10 + tn % 10;
if (rn == n) printf("%d is palindrome\n", n);
else printf("%d is not palindrome\n", n);
}
A loop like this might do:
int src; // user input
int n; // no of digits
int res = 0;
int tmp; // copy of src
// .... read the input: n and src ....
tmp = src;
for(int i = 0; i < n; i ++)
{
int digit = tmp % 10; // extract the rightmost digit
tmp /= 10; // and remove it from source
res = 10*res + digit; // apend it to the result
}
// ...and test if(res == src)...
I have to create a recursive function that tells you the number of ways a number of cents can be made into change. (Using quarters, dimes nickels, and pennies).
So far, I have a recursive function that does that, however it counts the same combination more than once, so the number is too big. How do I remove the duplicate combinations?
Code:
#include <stdio.h>
//Prototypes
int coins(int);
int main(void){
//Declarations
int num;
//Get user input
printf("Enter an amount of change in cents: ");
scanf("%d", &num); //Change to fgets
//Call function
printf("There are %d ways to make change for %d cents.\n", (coins(num)), num);
}
int coins(int amt){
//Declarations
int ways=0;
//Base Case
if(amt == 0){
return 1;
}
//int ways=0; More efficient after base case.
if(amt >= 1){
ways+=coins(amt-1);
}
if(amt >= 5){
ways+=coins(amt-5);
}
if(amt >= 10){
ways+=coins(amt-10);
}
if(amt >= 25){
ways+=coins(amt-25);
}
return ways;
}
Example:
Input: 17 (cents)
Output: 80 ways
**Output should be 6
#include <stdio.h>
int coins(int, int);
int main(void){
int num;
printf("Enter an amount of change in cents: ");
scanf("%d", &num);
printf("There are %d ways to make change for %d cents.\n", coins(num, 0), num);
return 0;
}
int coins(int amt, int kind){
static int kinds[4] = {25, 10, 5, 1};
int ways=0, i, n;
if(kinds[kind] == 1)//always divisible
return 1;
n = amt / kinds[kind];
for(i = 0; i <= n; ++i)
ways+=coins(amt-kinds[kind]*i, kind + 1);
return ways;
}