/*Task 1: Auction House*/
#include< stdio.h >
#include< stdbool.h >
#define minBid 100.00
//Putting all function headers to top
struct product scanProdInfo();
void printProdInfo(struct product p);
bool checkValid(int amt);
//Define struct product
struct product
{
int ID;
float bid_amt;
};
int main()
{
int num_prod; //to save the number of products
float total = 0; //variable for the total
printf("Welcome to the Auction House.\n");
printf("Enter number of products you want to bid on: ");
scanf("%d", &num_prod);
//create an array of struct to save all the products
struct product bidProds[total];
/*call the function to get 1 product info
and save it to the array struct of products
1 line loop body doesnt require {}*/
for(int prod_idx = 0; prod_idx < __20__; prod_idx++)
__21__[__22__] = scanProdInfo();
printf("\nThank you for your bids. Printing back: \n");
for(int i = 0; i < __23__; i++)
{
//call the function to print one product
printProdInfo(__24__[__25__]);
//add the product's amount to the total
total += __26__[__27__].__28__;
}
//Print the total outside of loop
printf("Your total bid amount is $%.2f.\n", __29__);
return 0;
}
//get the information fo one product
struct product scanProdInfo()
{
struct prod p;
printf("Enter 2 values for the product you're looking for (ID, then amt): ");
scanf("%d, %f", p.ID, p.bid_amt);
bool isValid = checkValid(__35__); //check if the inputed amount is valid
//ask user to re-enter if the input is valid (bid too low)
while(__36__)
{
printf("Your bid is too low for product #%d.", p.ID);
printf(" Re-enter the amount: ");
scanf("__37__", __38__);
__39__ = checkValid(__40__);
}
return __41__; //return the product
}
//print one product's info
void printProdInfo(__43__ __44__ pr)
{
printf("Product ID #%d: $%.2f.\n", __45__, __46__);
}
/*Check if the amount entered is not less than the mininum bid
and return true or false accordingly*/
bool checkValid(__48__ amt)
{
return(amt __49__ __50__) ? true : false;
}
/*Task 2: Copy Strings Backward
Input String1: Hello World*/
#include< stdio.h >
#define str_len 12 //str_len = hello + space + world + null char LEN
__2__ print_string(__3__ str[__4__]);
__5__ copy_string(__6__ s1[__7__], __8__ s2[__9__]);
int main()
{
__10__ str1[]= "Hello World";
__11__ str2[__12__];
//loop and copy each character over
// until the array reaches null character
copy_string(__13__, __14__);
//printing back by calling function print_string
printf("String1: ");
print_string(__15__);
printf("\nString2: ");
print_string(__16__);
}
/*Copy one string to another by looping backward.
Need 2 index variables to keep track of each string.
For ex: string1 = "hello", len = 6 (1 for null character)
string2[0] = string1[4] // letter 'o'
string2[1] = string2[3] // letter 'l'
Notice the index value compare to the len.*/
__17__ copy_string(__18__ s1[__19__], __20__ s2[__21__])
{
int idx1, idx2 = 0; //this is the index counter for the 2 strings s1 and s2
/*be careful here. We'll loop from the end of str1 to get letter 'd'
normally, for array, last character is size-1, but for string,
the last index is the null character, not the last character*/
for(idx1= str_len - __22__; idx1 >= __23__ ; idx1 __24__)
{
/*Copy each char from string s1 to string s2*/
__25__[__26__] = __27__[__28__];
/*idx1 for s1 is updated with for loop
update idx2 to save in the next location of s2.*/
idx2++;
}
//After reading all characters,need to add something in the last
//index position to let the program knows the string has ended.
s2[__29__] = '__30__';
}
/*
Printing all letter until the null character is reached.
printf("%s", str) would work, but we are doing this way
to make sure we understand how string works.
Loop through all character until it reaches null character*/
__31__ print_string(__32__ str[str_len])
{
for(int i = 0; str[i] != __33__; i++)
printf("%c", __34__);
}
#include< stdio.h >
#define length 70
//Putting all function headers to top
void print_scanf();
void print_getchar();
int main()
{
//print 2 sentences using getchar()
for(int i = 0; i < 2; i++)
{
print_getchar();
}
//print 2 sentences using scanf()
for(int i = 0; i < 2; i++)
{
print_scanf();
}
return 0;
}
//Function to print using getchar
void print_getchar()
{
char str[length]; //create a string called str with length defined above
printf("Enter your sentence for getchar:");
int i = 0;
while(1) //run forever,unless there's a break or return
{
/*save user's input to curChar since we need to check 2 cases:
Case 1. if the inputted character is a letter
we save curChar to our string (array of char)
Case 2. if it's a new line character.
We know the sentence is done, thus, the user press Enter
- > Done reading the string (or letters). Get out of loop*/
char curChar = getchar();
if(curChar == __6__) __7__; //case 2
__8__ = __9__; //case 1
i++;
}
//After reading all characters,need to add something in the last
//index position to let the program knows the string has ended.
str[i] = \0;
//print back the string
printf("Here's your sentence with getchar:\n");
printf("%s\n", __11__);
}
//Function to print using scanf
void print_scanf()
{
char str[length];//create a string called str
printf("Enter your sentence for scanf:");
scanf("__15__", &str[i]);//save to string str
//print back the string
printf("Here's your sentence using scanf:\n");
printf("__17__\n", &str[i]);
}