Expected an expression; menu driven function program - c

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');
}

Related

C program How to get return value from other function by using switch statement

So I am trying to do a program where it shows a menu and asks what will the user do. I am stuck in where I cant get the return value in case 1 and use it in case 5. I am only a beginner and need help.
This is what I have in my program.
#include <stdio.h>
int choice, arrayval, val;
char reply, y, n;
void menu()
{
printf("\n\n");
printf("MAIN MENU");
printf("\n\n");
printf("[1] - Store/Fill Array\n");
printf("[2] - Find and Replace\n");
printf("[3] - Display Frequency\n");
printf("[4] - Unique\n");
printf("[5] - Print\n");
printf("[0] - Print\n");
printf("Enter your choice: ");
scanf(" %s", &choice);
}
int storearray()
{
int arrayval[10];
int i;
for(i=0;i<10;i++)
{
printf("Enter Value for Array[%d]", i);
scanf("%d", &arrayval[i]);
}
return arrayval;
}
void printarr(arrayval)
{
int i;
for(i=0;i<10;i++)
{
printf("Array[%d] %d\n", i, arrayval);
}
}
int main()
{
do
{
menu();
switch(choice)
{
case '1':
storearray();
main();
break;
case '5':
val = storearray();
printarr(val);
return 0;
break;
default:
printf("Invalid");
}
}
while (choice != 1,2,3,4,5);
printf("End of Program!");
return 0;
}
need help on how to get the value from storearray() and use it to print in printarr()
Here's a modified version of your code with the fixes mentioned in the comments of your question:
#include <stdio.h>
#include<stdlib.h>
char choice;
int *val;
char reply, y, n;
void menu()
{
printf("\n\n");
printf("MAIN MENU");
printf("\n\n");
printf("[1] - Store/Fill Array\n");
printf("[2] - Find and Replace\n");
printf("[3] - Display Frequency\n");
printf("[4] - Unique\n");
printf("[5] - Print\n");
printf("[0] - Print\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
}
int* storearray()
{
int *arrayval=malloc(10*sizeof(int));
int i;
for (i = 0; i < 10; i++)
{
printf("Enter Value for Array[%d]", i);
scanf("%d", &arrayval[i]);
}
return arrayval;
}
void printarr(int *arrayval)
{
int i;
for (i = 0; i < 10; i++)
{
printf("Array[%d] %d\n", i, arrayval[i]);
}
}
int main(void){
val=NULL;
int inv=1;
do
{
menu();
switch (choice)
{
case '1':
val=storearray(); //be sure to `free(val)` (if `val` is pointing to a malloc'ed memory) before you do `val=storearray()` or you'll lose the pointer and it'll result in a memory leak
break;
case '5':
//I'd also recommend a check here to ensure `val` isn't NULL
printarr(val);
inv=0;
break;
default:
printf("Invalid");
inv=0;
break;
}
} while (inv);
printf("End of Program!");
free(val);
return 0;
}
Also, as chux mentioned in the comment:
"I am only a beginner and need help." --> Simple tip: enable all compiler warnings. Faster feedback than posting stack overflow.

unknown error in c. this program not going to void Linear_Search() function

/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.

Loop Run Wrong In Main Function

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");

Copying elements of array to a new one - C

I'm currently doing a school assignment and now I'm really stuck. The problem I have is that When I'm trying to copy the elements of the array dice to the array diceCheck the program goes in to some kind of infinite loop and I don't understand why. Help would be greatly appreciated!
Edit: It's in the bottom in the function printScores.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printMenu(void);
void throwDice(int dice[], int nrOfDice, int nrOfDieValues);
void readDieValues (int dice[], int nrOfDice);
void printDice(const int dice[], int nrOfDice);
void printScores(const int dice[], int nrOfdice, int nrOfDieValues);
int isThreeOfAKind(const int dieValues[], int nrOfDieValues);
int isSmallStraight(const int dieValues[], int nrOfDieValues);
int main(void)
{
const int nrOfDice = 5;
const int nrOfDieValues = 6;
int dice[4], menuChoice = 0;
printMenu();
printf("\nMake your choice: ");
while(scanf("%d", &menuChoice) != -1)
{
switch (menuChoice)
{
case 0:
printMenu();
break;
case 1:
throwDice(dice, nrOfDice, nrOfDieValues);
printf("Make your choice: ");
break;
case 2:
readDieValues(dice, nrOfDice);
printf("Make your choice: ");
break;
case 3:
printDice(dice, nrOfDice);
printf("Make your choice: ");
break;
case 4:
printScores(dice, nrOfDice, nrOfDieValues);
break;
case -1:
return 0;
break;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
void printMenu()
{
printf("MENU:\n");
printf("0. Display the menu\n");
printf("1. Make a random throw\n");
printf("2. Enter die values for a throw\n");
printf("3. Display the die values for the throw\n");
printf("4. Display the score for the throw\n");
printf("-1. End program\n");
}
void throwDice(int dice[], int nrOfDice, int nrOfDieValues)
{
int choice, i;
printf("Enter seed (1 gives a random seed): ");
scanf("%d", &choice);
if(choice == 1)
{
srand(time(NULL));
for (i = 0; i < nrOfDice; i++)
{
dice[i] = ( rand() % nrOfDieValues) + 1;
}
printf("\n");
}
else
{
srand(choice);
for(i = 0; i < nrOfDice; i++)
{
dice[i] = ( rand() % nrOfDieValues) + 1;
}
printf("\n");
}
}
void readDieValues(int dice[], int nrOfDice)
{
int i;
for(i = 0; i < nrOfDice; i++)
{
printf("Die %d: ", (i+1));
scanf("%d", &dice[i]);
}
}
void printDice(const int dice[], int nrOfDice)
{
int i;
printf("Your dice: ");
for(i = 0; i < nrOfDice; i++)
{
printf("%d ", dice[i]);
}
printf("\n\n");
}
int isThreeOfAKind(const int dieValues[], int nrOfDieValues)
{
}
int isSmallStraight(const int dieValues[], int nrOfDieValues)
{
}
void printScores(const int dice[], int nrOfdice, int nrOfDieValues)
{
int diceCheck[4], i;
for(i = 0; i < nrOfdice; i++)
{
diceCheck[i] = dice[i];
printf("%d ", dice[i]); //these are just for myself to check if it worked
printf("%d ", diceCheck[i]); //these are just for myself to check if it worked
}
}
You have:
const int nrOfDice = 5;
but
int dice[4];
int diceCheck[4];
Your copying idea is correct but you are going one past the end of the array.
To avoid this sort of error, initialize both from the same expression, e.g.:
int dice[4];
const int nrOfDice = sizeof dice / sizeof dice[0];
or
const int nrOfDice = 4;
int dice[nrOfDice];
and inside the PrintScores function, instead of int diceCheck[4];, do:
int diceCheck[nrOfDice];

assistance with finding the High and low of an array and setting an array equal to zero

The problem i have is even though i set the array called exam[] to zero in main, when i call the function displayAllExam() only some are set to zero and others are set to random numbers that probably represent spaces in ram. This is messing up my displayExamAverage() function because when i run the code i can add values to the array but if i dont add 50 values then there will be random numbers in there. Also im really stuck on the displayHighLow function which is ment to find the highest and lowest value of the array
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define pause system("pause")
#define cls system("cls")
#define flush fflush(stdin)
#define SIZE 50
//Lets Prototype
int getChoice();
void displayMenu();
void addScores(int a[],int *c);
void displayAllExam(int a[]);
void displayExamAverage(int a[], int *c);
void displayHighLow(int a[], int *c);
main(){
int exams[SIZE] = {0}, counter = 0;
char choice;
do {
choice = getChoice();
switch(choice){
case 1:
addScores(&exams[SIZE],&counter);
break;
case 2:
displayExamAverage(&exams[SIZE],&counter);
break;
case 3:
displayHighLow(&exams[SIZE], &counter);
break;
case 4:
displayAllExam(&exams[SIZE]);
break;
}
}while (choice != 5);
}//end main
int getChoice(){
int result = 0;
do{
displayMenu();
scanf_s("%i", &result);
if(result < 1 || result >5 ){
printf("Invalid Selection, please enter a number between 1 and 5\n");
pause;}
} while(result < 1 || result > 5);
return result;
}//end getChoice
void displayMenu() {
cls;
printf("MAIN MENU\n\n");
printf("1: add an exam score\n");
printf("2.Display exam average\n");
printf("3.Display High and Low score\n");
printf("4.Display all scores\n");
printf("5.Quit\n\n");
printf("Enter your choice: \n\n");
return;
}//end display menu
void addScores(int a[SIZE],int *c){
int i=0;
printf("please enter your score..");
scanf_s("%i", &a[i]);
}//end add scores
void displayExamAverage(int a[],int *c){
float average, total = 0.0;
int i;
for(i=0; i < *c; i++)
total = total + a[i];
average = total/ *c;
printf("%f",average);
pause;
}//end displayExamAverage
void displayHighLow(int a[],int *c){
int high= a[0], low= a[0];
int i,x,y;
for(i=0; i < *c; i++){
if(a[*c]> a[0])
high = a[i];
}
for(i=0; i < *c; i++){
if(a[*c]<a[0])
low = a[i];
}
printf("The low is %i", low);
printf("The high is %i", high);
pause;
}//end DisplayHighLow
void displayAllExam(int a[SIZE]){
int i;
for(i=0;i < SIZE; i++)
printf("the %i score is %i\n",i+1,a[i]);
pause;
}//end displayAllExam
You're passing a pointer to the position just after the end of the array. That's a big no-no, if you plan to read or write to it:
displayAllExam(&exams[SIZE]);
Instead, just pass it like this:
displayAllExam(exams);
Same goes for all your other calls.

Resources