When i run the code,in main function something going wrong.After first round of loop in main,program print "Wrong Choice" altough choice is legal.
#include <stdio.h>
#include <stdlib.h>
int factorial(int n);
void prime_numbers(){
int upper_bound;
printf("Define upper bound:");
scanf("%d",&upper_bound);
printf("Prime Numbers: ");
int i,j;
int variable;
for (i=2;i<=upper_bound;i++)
{
variable = 1;
for (j = 2; j <i; j++)
{ if (i % j == 0)
{
variable = 0;
break;
}
}
if (variable == 1)
printf ("%d ", i );
}
printf("\n");
}
void leibniz_series(){
printf("Define k value:");
int k;
scanf("%d",&k);
double sum=0;
int i;
for (i=1; i<k; i++)
{
if ((i%2)==1)
sum=sum+1.0/((2.0 * (double)i) - 1.0);
else
sum = sum - 1.0/((2.0 * (double)i) - 1.0);
}
printf("Result:%f\n",4*sum);
}
void combination(){
int comb;
printf("Define n and r:");
int n,r;
scanf("%d %d",&n,&r);
comb=factorial(n)/(factorial(r)*factorial(n-r));
printf("Result:%d ",comb);
printf("\n");
}
int factorial(int n){
int f=1;
int i;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
int main()
{
printf("Press P or p for Prime Numbers\n");
printf("Press L or l for Leibniz Series\n");
printf("Press C or c for Combinations\n");
printf("Press E or e for Exit\n");
char choice;
for(;;){
printf("Enter your choice:");
scanf("%c",&choice);
if(choice=='E' || choice=='e')
exit(0);
switch(choice){
case 'p' : prime_numbers();
break;
case 'P' : prime_numbers();
break;
case 'l' : leibniz_series();
break;
case 'L' : leibniz_series();
break;
case 'c' : combination();
break;
case 'C' : combination();
break;
default : printf("Wrong choice\n");
}
}
return 0;
}
It is probably something to do with the return key you pressed to enter the input lying around in your input buffer.
To simply clear that character from the buffer you can read it and ignore it by adding scanf("%*c"); immediately after you scan for a character from the STDIN.
So your new code would look something like this:
printf("Enter your choice:");
scanf("%c",&choice);
scanf("%*c");
Related
Like i want to put the "return 0" at the right place, everything in the code is working normal.
complie the code and they keep saying about "while before return", i tried to put outside of the code and inside
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int menu(){
printf("Menu:\n");
printf("1- Add a value\n");
printf("2- Search a value\n");
printf("3- Print out the array\n");
printf("4- Print out values in a range\n");
printf("5- Print out the array in ascending order\n");
printf("6- Quit?\n");
printf("Enter your operation: ");
int choice;
scanf("%d", &choice);
return choice;
}
int isFul (int*a, int n){
return n==MAXN;
}
int isEmpty (int*a, int n){
return n==0;
}
void add(int value, int*a, int*pn){
a[*pn] = value;
(*pn)++;
}
int search(int x, int *a, int n){
int i;
for (i=0; i<n; i++) if (a[i]==x) return i;
return -1;
}
void printvalueinrange (int*a, int n){
int i, min, max;
printf("\nEnter min value: ");scanf("%d", &min);
printf("\nEnter max value: ");scanf("%d", &max);
for(i=0; i<sizeof(a); i++)
if(a[i]>=min&&a[i]<=max) printf("%d", a[i]);
}
void printarray(int*a, int n){
int i;
for (i=0;i<n;i++) printf("%d", a[i]);
}
void printAsc(int*a, int n){
int** adds =(int**)calloc(n, sizeof(int*));
int i,j;
for(i=0; i<n; i++) adds[i]= &a[i];
int* t;
for (i=0;i<n-1; i++)
for(j=n-1; j>i; j--)
if (*adds[j]< *adds[j-i]){
t=adds[j];
adds[j]=adds[j-1];
adds[j-1]=t;
}
for (i=0;i<n; i++) printf("%d ", *adds[i]);
free(adds);
}
int main(){
int a[MAXN];
int n=0;
int value;
int choice;
do
{ choice= menu();
switch(choice){
case 1:{
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
else
{ printf ("Input an added value:");
scanf("%d", &value);
add(value, a, &n);
printf("Added!\n");
}
break;
}
case 2:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{ printf ("Input the searched value:");
scanf("%d", &value);
int pos = search(value, a, n);
if (pos<0) printf("Not found!\n");
else printf("Postion is found: %d\n", pos);
} break;
}
case 3:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printarray(a,n);
} break;
}
case 4:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printvalueinrange(a,n);
} break;
}
case 5:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printAsc(a,n);
} break;
}
default: printf ("Goodbye!");
break;
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
I just adding some word to fit with the requirement :"D
And if u find out any kind of error or mistake that in my code, just points out for me to improve them xd
In this part of main
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
the while construction occupies a wrong place.
There should be
}
} while (choice>0 && choice<7);
getchar();
return 0;
}
Also in this statement
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
^^^^^^
there is a typo. There should be
if (isFul(a,n)) printf("\n Sorry! The array is full.\n");
When the "do you wish to quit" question/main function is asked an answered with 'n' I need the code to then ask the question again and again until the user is done with the program. Aka repeating for as ever long as necessary
int getNumTerms()
{
int n;
printf("Enter the number of terms: \n");
scanf("%d",&n);
return n;
}
double coshyper (double x, int n)
{
int i;
double sum=1, term=1;
for (i=1; i<n; i++)
{
term=(x*x/(2.0*(double)i*(2.0*(double)i-1.0)))*term;
sum=sum+term;
}
return sum;
}
int main()
{
char q;
printf("Do you wish to quit(y/n)?\n");
scanf("%c",&q);
if(q=='y')
{
printf("Program Terminated\n");
}
while(q=='n')
{
int n;
double x;
printf("Enter x: \n");
scanf("%lf",&x);
n=getNumTerms();
double result = coshyper(x,n);
printf("The value for cosh(%.3lf) for %d terms is %.6lf\n",x,n,result);
}
}
You can change the main to something like this:
int main()
{
char q = 'n';
do{
if(q == 'y'){
printf("Program Terminated\n");
break;
}
if(q == 'n'){
int n;
double x;
printf("Enter x: \n");
scanf("%lf",&x);
n=getNumTerms();
double result = coshyper(x,n);
printf("The value for cosh(%.3lf) for %d terms is %.6lf\n",x,n,result);
}
else
{
printf("Invalid");
}
printf("Do you wish to quit(y/n)?\n");
scanf("%c",&q);
}while(1);
return 0;
}
/int arr[n]; int n;/
void Linear_Search() {
int i, q, flag = 0, num, n;
int arr[n];
printf("Enter the number of array\n");
scanf("%d", n);
printf("Enter the numbers from which searched\n");
for (i = 0; i <= n; i++); {
scanf("%d", & arr[i]);
}
printf("enter the number to be searched\n");
scanf("%d", & q);
for (i = 0; i <= n; i++) {
if (q == arr[i]); {
num = q;
flag = 1;
}
}
if (flag == 1) printf("found! number is %d", num);
else printf("number not present in group\n");
getch();
}
void main() {
printf("ALL SEARCHING TECHNIQUE\n");
printf("Choices\n");
printf("1.Linear Search\n2.Binary Search\n3.Interpolation Search\n4.Jump Search\n");
void Linear_Search();
int select, l = 1;
scanf("%d", & select);
switch (select) {
case 1:
printf("This is Linear Search\n");
void Linear_Search();
break;
}
/case 2: printf("This is Binary Search\n"); void Binary_Search(); break; case 3: printf("This is Interpolation Search\n"); void Interpolation_Search(); break; case 4: printf("This is Jump Search\n"); void Jump_Search(); break; } printf("To continue PRESS 1 or PRESS ANY KEY"); scanf("%d",&l);/
getch();
}
You are including the return type of the function at the places where you are trying to call them. This turns them from a function call to a function prototype declaration. Remove the return type (void in these cases) at the places where the function shall be called.
I'm having problems calling the function displayArray. It says expected an expression. What am I doing wrong? I don't quite fully understand how calling a function works, any help is appreciated. I'm still working on this program and my only problem right now is calling functions.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH myFlush()
#define SIZE 100
#define LB 1
#define UB 100
// ProtoType Functions Here
void myFlush();
int loadArray(int num[], int c);
void displayArray(int num[], int c);
void enterAnumber();
main() {
int userChoice = 0;
do {
CLS;
printf("=======================\n");
printf("====== MAIN MENU ======\n");
printf("=======================\n");
printf("1. Enter a number \n");
printf("2. Display the sum of all numbers entered \n");
printf("3. Display Average of numbers entered \n");
printf("4. Display all numbers entered \n\n");
printf("5. Quit the program \n\n");
printf("Enter your selection: ");
scanf("%i", &userChoice);
switch (userChoice) {
case 1: // Enter a number
enterAnumber();
PAUSE;
break;
case 2: // Display the sum of all nums ent
PAUSE;
break;
case 3: // Display the average of all nums ent
PAUSE;
break;
case 4: // Display all numbers entered
displayArray(int num[], int c);
PAUSE;
break;
case 5: // QUIT THE PROGRAM
PAUSE;
break;
default:
PAUSE;
break;
} // end switch
} while (userChoice != 4);
PAUSE;
} // end of main
//============================================
void enterAnumber() {
int numbers[SIZE];
int count = 0;
// seed the rand function
srand((unsigned)time(NULL));
// displayArray(numbers, count); // ArrayStats
count = loadArray(numbers, count);
}
void displayArray(int num[], int c) {
int i;
for (i = 0; i < c; i++) {
printf("The number selected: %i\n", num[i]);
}
PAUSE;
}
int loadArray(int num[], int c) {
int result;
int i;
int howMany;
printf("How many numbers do you wish to create: ");
scanf("%i", &howMany);
for (i = 0; i < howMany; i++)
num[i] = LB + rand() % (UB - LB + 1);
result = c + howMany;
return result;
} // end loadArray
void myFlush() {
while (getchar() != '\n');
}
I have just completed a simple array operation in C. I have done the programming consisting of a number of functions all performing the insert operation at various locations. The main problem that I'm facing is that the control is not returning to main after I have performed the operation with a function . I can't find the reason. Just explain why is this happening and provide me with a proper solution. My code is like this:
//To insert an element into an already formed array
#include<stdio.h>
#include<stdlib.h>
int binsert(int[],int);
int minsert(int[],int,int);
int einsert(int[],int);
void print(int[],int);
void main()
{
int a[100],i,j,l,loc;
printf("Enter the length of the array\n");
scanf("%d",&l);
printf("Enter the numbers in the array\n");
for(j=0;j<l;j++)
{
scanf("%d",&a[j]);
}
printf("Enter the following options for the operation\n1:insert at the beginning\n2:insert at specific location\n3:insert at the end\n4:print the array\nAny other key to end the operation\n");
scanf("%d",&i);
do
{
switch(i)
{
case 1:
l=binsert(a,l);
break;
case 2:
printf("Enter the location at which you would want to insert(location starts from 1)\n");
scanf("%d",&loc);
l=minsert(a,loc,l);
break;
case 3:
l=einsert(a,l);
break;
case 4:
print(a,l);
break;
}
}while(i==1 || i==2 || i==3 || i==4);
printf("Operation terminated\n");
exit(0);
}
int binsert(int a[],int l)
{
int i;
for(i=l-1;i>=0;i--)
{
a[i+1]=a[i];
}
printf("Enter the number to insert\n");
scanf("%d",&a[0]);
return (l+1);
}
int minsert(int a[],int loc,int l)
{
int i;
for(i=l-1;i>=loc;i--)
{
a[i+1]=a[i];
}
printf("Enter the number to insert\n");
scanf("%d",&a[loc]);
return (l+1);
}
int einsert(int a[],int l)
{
printf("Enter the number to insert\n");
scanf("%d",&a[l+1]);
return (l+1);
}
void print(int a[],int l)
{
int i;
printf("The values in the array are as follows\n");
for(i=0;i<l;i++)
{
printf("%d\n",a[i]);
}
}
PROBLEM 1 :
you should have done :
do
{
printf("Enter the following options for the operation\n1:insert at the beginning\n2:insert at specific location\n3:insert at the end\n4:print the array\nAny other key to end the operation\n");
scanf("%d",&i);
switch(i)
{
....
}
}while(i==1 || i==2 || i==3 || i==4);
otherwise , i will always contain the same value i.e among 1 , 2, 3, 4, and while(i==1 || i==2 || i==3 || i==4); will always be true , and so same function will be called indefinitely.
PROBLEM 2: have a check on l , so that it do not exceed 100 , size of your array .
What you think?
//To insert an element into an already formed array
#include<stdio.h>
#include<stdlib.h>
int binsert(int[],int);
int minsert(int[],int,int);
int einsert(int[],int);
void print(int[],int);
void main()
{
int a[100],i,j,l,loc;
for(i=0; i < 100; i++)
a[i] = -1;
printf("Enter the length of the array\n");
scanf("%d%*c",&l);
for(j=0; j<l; j++)
{
system("cls");
printf("Enter the numbers in the array\n");
for(i = 0; i < j; i++)
printf("%d ", a[i]);
scanf("%d%*c",&a[j]);
}
do
{
system("cls");
printf("Array: ");
for(j = 0; j < (sizeof(a)/sizeof(a[0])) && a[j] != -1; j++)
printf("%d ", a[j]);
printf("\n\n\nEnter the following options for the operation\n\n\t1:insert at the beginning\n\t2:insert at specific location\n\t3:insert at the end\n\t4:print the array\n\tAny other key to end the operation\n\nOption: ");
scanf("%d%*c",&i);
switch(i)
{
case 1:
l=binsert(a,l);
break;
case 2:
printf("Enter the location at which you would want to insert(location starts from 1)\n");
scanf("%d",&loc);
l=minsert(a,loc,l);
break;
case 3:
l=einsert(a,l);
break;
case 4:
print(a,l);
break;
}
}
while(i==1 || i==2 || i==3 || i==4);
printf("\n\nOperation terminated\n");
getchar();
exit(0);
}
int binsert(int a[],int l)
{
int i;
for(i=l-1; i>=0; i--)
{
a[i+1]=a[i];
}
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[0]);
return (l+1);
}
int minsert(int a[],int loc,int l)
{
int i;
for(i=l-1; i>=loc; i--)
{
a[i+1]=a[i];
}
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[loc]);
return (l+1);
}
int einsert(int a[],int l)
{
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[l+1]);
return (l+1);
}
void print(int a[],int l)
{
int i;
printf("\n\nThe values in the array are as follows\n");
for(i=0; i<l; i++)
{
printf("%d\n",a[i]);
}
}
I think below line is causing problem
for(i=l-1;i>=0;i--)
{a[i+1]=a[i];
}
It's like when i becomes 0 it will execute and then i-- will cause it to be -1 and negative numbers are stored in 2's complement so it will be positive number and condition i>=0 is satisfied every time.
Hope this might help you
Thanks