Lefttriangle(), Righttringle(), Pascaltriangle() - c

Q : Write three different functions Lefttriangle(),Righttriangle(),Pascaltriangle() in a single C-program and display the triangle as asked by the user interactively.
So I have tried this one. It is printing the options, then I am entering an option, suppose 1 then it is asking for the number "n". Then ,I am entering the number, suppose 4 and pressing enter. But then it isn't showing the left triangle corresponding to 4. It is printing the options again.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void pascaltriangle(int i,int j,int k,int n,int m)
{
for(i=0;i<n;i++)
{
for(k=1;k<=n-i;k++)
{
printf("The Pascal triangle of numbers is as follows - \n");
printf(" ");
}
for(j=0;j<=i;j++)
{
if(j==0||i==0)
m=1;
else
m=m*(i-j+1)/j;
printf(" %d",m);
}
printf("\n",m);
}
}
void lefttriangle(int i,int j,int n)
{
printf("Enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("The left triangle of numbers is as follows - \n");
printf("%d",j);
printf("\n");
}
}
void righttriangle(int i,int j,int k,int n)
{
printf("Enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
printf("The right triangle of numbers is as follows - \n");
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%d",k);
}
printf("\n");
}
}
int main()
{
int choice;
int i,j,k,n, m=1;
do
{
printf("\nEnter the choice below.\n");
printf("*************************\n");
printf("1-> Left Traingle.\n");
printf("2-> Right Triangle.\n");
printf("3-> Pascal Triangle.\n");
printf("*************************\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value of n : ");
scanf("%d",&n);
void lefttriangle( i,j, n);
break;
case 2:
printf("Enter the value of n : ");
scanf("%d",&n);
void righttriangle(i,j,k,n);
break;
case 3:
printf("Enter the value of n : ");
scanf("%d",&n);
void pascaltriangle(i, j, k, n, m);
break;
case 4:
printf("Thank you!\n");
exit(0);
default:
printf("Enter a valid number.\n");
}
}while(1);
return(0);
}

In your switch, when 4 is entered, that case corresponds to:
printf("Thank you!\n");
exit(0);
Only if the user enters 1 will the left triangle function be called.
Further, when calling these functions, you don't put the return type in front of them. You only do that when declaring and/or implementing them.

Related

scanf in function seems to be writing garbage values to one array but not the other, despite both being identical

So in the function getRoster, I have two arrays that are defined in the same way in main and called in the same way. But for some reason, one of the scanf functions write garbage data to the array so that in line 91 (A debugging line in this case) one value is the one I entered and the other is seemingly random. I've checked this code top to bottom already to see if there were any notations that I added to one array and not the other, and I can't seem to find a single one.
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers[10], int *playerRatings[10])
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1);
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers[10], int *playerRatings[10])
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int *jerseyNumbers[10];
int *playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(&jerseyNumbers[10], &playerRatings[10]);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(&jerseyNumbers[10], &playerRatings[10]);
return 0;
}
Refer to #NaveenKumar and #DeiDei's comment to your question.
Actually, the logic of your program is alright. But where you've gone wrong is the syntax regarding how you have declared the arrays and passed them as arguments to the functions. I have listed the changes to be made below:
First of all Dylan, do not declare an array as: int
*jerseyNumbers[10] and int *playerRatings[10]. For the regular array which you actually need, just declare as:
a. *jerseyNumbers and *playerRatings... Remove the size. OR
b. jerseyNumbers[10] and playerRatings[10]... Remove the * from the declaration.
When you're passing these arrays as arguments to a function, don't ever send it how you've done. Just pass the array name as an argument. Like this: getRoster(jerseyNumbers, playerRatings); and menu(jerseyNumbers, playerRatings);.
In the function definition, the parameters representing the array should be either *arrayname or arrayname[size]. Since we have used the first during declaration, use the same here as follows:
void getRoster(int *jerseyNumbers, int *playerRatings) {...},
void updateRating(int *jerseyNumbers, int *playerRatings){...},
void outputRoster(int *jerseyNumbers, int *playerRatings) {...},
void menu(int *jerseyNumbers, int *playerRatings) {...}.
And as #PaulSm4 has suggested don't use the semi-colon at the end of an if conditional. Though this isn't the cause of your problem here, it is a practice you need to follow and mistake to be avoided.
I have attached the working code below, along with the output.
CODE:
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1)
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers, int *playerRatings)
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(jerseyNumbers, playerRatings);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(jerseyNumbers, playerRatings);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int jerseyNumbers[10];
int playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(jerseyNumbers, playerRatings);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(jerseyNumbers, playerRatings);
return 0;
}
OUTPUT:
Enter player 1's jersey number
1
Enter player 1's rating
10
Enter player 2's jersey number
2
Enter player 2's rating
20
Enter player 3's jersey number
3
Enter player 3's rating
30
Enter player 4's jersey number
4
Enter player 4's rating
40
Enter player 5's jersey number
5
Enter player 5's rating
50
1 20
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: o
Player 1 -- Jersey number: 1 , Rating: 10
Player 2 -- Jersey number: 2 , Rating: 20
Player 3 -- Jersey number: 3 , Rating: 30
Player 4 -- Jersey number: 4 , Rating: 40
Player 5 -- Jersey number: 5 , Rating: 50
Hope this helps.
STRONG SUGGESTION:
Get in the habit of using curly braces often - even when you don't need them.
void updateRating(int *jerseyNumbers, int *playerRatings)
// You probably only need a pointer or an array - but probably not both ;)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i) { // Curly brace here...
if(jerseyNumbers[i]==n) { // And here...
hold = n;
}
}
if (n=-1) { // BUG ALERT: deleted ";" (and added a brace)
printf("Error"); // BUG ALERT: fixed this by removing ";"?
} else {
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
Or perhaps better:
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int n;
printf("Enter a jersey number:\n");
scanf("%d", n);
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
And, as Rishikesh Raje correctly pointed out:
The definition of the array's and the function calls for OP is also
not correct. Please mention this also.

C: Stack implementation

following code for stack implementation, produces runtime-errors, although it does compile .. Can anyone help me identify find the error?
#include<stdio.h>
#define size 5
// declared a structure for stack which has an int array and top as it's members..
struct stack{
int a[size],top;
}s;
// following method pushes element 'item' in stack when called after checking if stack-overflows or not?
void push(int item){
if(s.top >= size-1)
printf("\nStack overflow..\n");
else
s.a[++s.top] = item;
}
following method pops one element when called..
int pop(){
if(s.top == -1){
printf("\n..Stack underflow..\n");
return 0;
}
return s.a[s.top];
}
// displays elements in the stack till a[top]
void display(){
int i;
for(i = s.top; i>=0; i--){
printf("\n%d", &s.a[i]);
}
}
// main() method..
int main(){
s.top = -1;
int item, choice;
char ans;
printf(" ..Stack Implementation..\n");
printf("-----------------------------");
do{
printf("\nMain Menu");
printf("\n-------------");
printf("\n 1. Push\n 2. Pop\n 3. Display\n 4. Exit\n");
printf("\n Enter your choice: ");
scanf("%d", choice);
switch(choice){
case 1:
printf("\nEnter item to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
}
printf("\n Want to continue? :");
scanf("%c", &ans);
}while(ans == 'Y' || ans == 'y');
return 0;
}
There is a small typo [I think so, as your other occurrences are correct]. Change
scanf("%d", choice);
to
scanf("%d", &choice);
Also, there is a small problem in scanf("%c", &ans);. It will suffer from the previously-pressed enter. Use
scanf(" %c", &ans); //mind the space before %
Other Issues: [Added after the edit]
printf("\n%d", &s.a[i]); -- get rid of the &, don't need that in printf()
return s.a[s.top]; should be return s.a[s.top--];

Inventory of a grocery store

I write a program to keep track of the inventory of a grocer store. But in my code i would like to print a value. And value is (Number of units) * (unit price). But somehow i get get the garbage value in my program. So Can you Please help me?
#include<stdio.h>
#include<conio.h>
#define MAX 5
int printinventory(int , int unit[] , float price[]);
int main()
{
int item[MAX],unit[MAX],x,i;
float price[MAX];
printf("Please enter how many category items (up to 5) : ");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("\nPlease enter Number of Units #%d : ",i+1);
scanf(" %d",&unit[i]);
printf("\nPlease enter the Unit Price #%d : ",i+1);
scanf(" %f",&price[i]);
}
printinventory(x , unit , price);
getch();
}
int printinventory (int y, int unit[] , float price[])
{
int i,j=0;
float value[MAX];
for(i=0;i<y;i++);
{
value[i] = (unit[i] * price[i]);
}
system("cls");
printf("Item Number of Units Unit Price Value ");
printf("\n\n------------------------------------------------");
for(i=1;i<=y;i++)
{
printf("\n%d",i);
printf("\t %d",unit[j]);
printf("\t\t $%.2f",price[j]);
printf("\t$%.2f",value[j]);
j++;
}
printf("\n\n------------------------------------------------");
printf("\n\t\t\t\tTotal $ ");
getch();
}
The problem seems to be that you've mistakenly included a semicolon at the end of one of your for loops:
for(i=0;i<y;i++);

static declaration of ‘DisplayMenu’ follows non-static declaration

Why do I keep getting this error? HELP ME this is homeowrk. I'm obiously new to programming help.
$ gcc homework.c
homework.c: In function ‘main’:
homework.c:32:6: error: static declaration of ‘DisplayMenu’ follows non-static declaration
homework.c:11:7: note: previous declaration of ‘DisplayMenu’ was here
#include <stdio.h>
void DisplayMenu();
void numberPlus10();
void numberTimes2();
void numberMinus1();
void numberTimesnumber();
int main (void)
{
int choice;
void DisplayMenu();
scanf("%i", &choice);
switch (choice)
{
case 1:
numberPlus10();
break;
case 2:
numberTimes2();
break;
case 3:
numberMinus1();
break;
case 4:
numberTimesnumber();
break;
default:
break;
}
void DisplayMenu()
{
printf("1. Number + 10\n");
printf("2. Number * 2\n");
printf("3. Number - 1\n");
printf("4. Number * Number\n");
}
void numberPlus10()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number + 10 is %i\n", x + 10);
}
void numberTimes2()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number * 2 is %i\n", x * 2);
}
void numberMinus1()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number - 1 is %i\n", x - 1);
}
void numberTimesnumber()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number squared is %i\n", x * x);
}
}
Pengyu CHEN is ofcourse right! But! You have another error there.
int choice;
void DisplayMenu(); // You should not declare a function here.
scanf("%i", &choice);
I guess you intend to call this function - so just remove "void" from the beginning of the line.
int choice;
DisplayMenu(); // Call DisplayMenu
scanf("%i", &choice);
And ... please read language specs
In C we don't implement functions inside any blocks. Instead functions shall be implemented in global scope.
Remove the very last right bracket and put it right after end of the switch in int main(void), and there shall be no more errors.
EDITED:
First of all.. I'm sure above is why your source code fails to compile.
Also, please check David's answer, since we all believe that you made a function declaration while you're intending to call it -- although this mistake didn't trigger an compile time error.

Switch case missing in C program

#include<stdio.h>
#include<conio.h>
void inversion(void);
void ways(void);
void prime(void);
void power(void);
void fibonacci(void);
void main(void)
{
char choice;
printf("Enter a choice:\n 1.Perform inversion of digits \n 2.calculate all the ways that a positive number can be get by adding \n 3.Calculate prime numbers in a range\n 4.Calculate power of a number. \n 5.generate a particular numebr of fibonacci");
choice==getche();
switch(choice)
{
case 1: inversion();
break;
case 2: ways();
break;
case 3: prime();
break;
case 4:power();
break;
case 5:fibonacci();
break;
}//switch ends
//printf("Do you want to perform this once more? (Y?N):");
//}
//while (choice=='y'||choice=='Y');
getch();
}
void inversion(void)
{
clrscr();
int num,i,x,y;
printf("Enter a 4 digit number:");
scanf("%d",&num);
for(i=1;i<=4;i++)
{
x=num%10;
y=num/10;
printf("%d",x);
num=y;
}// for ends
getch();
}
void ways(void)
{ clrscr();
int num,i,j;
printf("Enter the number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
if(i+j==num)
printf("%d+%d=%d",i,j,num);
getch();
}
void prime(void)
{
int num1,num2,i;
printf("Enter a range separated by space:");
scanf("%d %d",num1,num2);
for(i=2;i<num2;i++)
{ if(num1%i!=0)//is prime
printf("%d",num1);
}//for ends
getch();
}//prime ends
void power(void)
{
int num,index,i,result=1;
printf("Enter the number and its index (eg.2^3)");
scanf("%d %d",&num,index);
for(i=1;i<=index;i++)
result=num*result;
printf("%d",result);
}//power function ends
void fibonacci(void)
{
int num,x=1,y=1,i,z;
printf("Enter the term you want to find in fibonacci series:");
scanf("%d",&num);
if (num==1)
printf("it is the 1st & 2nd term");
for(i=1;i<=num+2;i++)
{
z=x+y;
if (i==num)
printf("%d",z);
x=y;
y=z;
}
printf("%d");
}//fibonacci func ends
There are a lot of problems in your code:
# I would like to suggest you to use getchar() instead of getche() for a character input.
# Now you are using a charcter for numeric options which you could have avoided by defining your choice variable as integer:
int choice;
scanf (%d, &choice);
# also if you were using char choice;
you are having cases as:
case 1: inversion();
break;
while your choice variable was char
so you should have case like:
case '1': inversion();
anyways if you will define choice as int then your code should work fine.
or you can change your cases also with case values in single quotes.

Resources