C Program - adding, removing, and printing dominos from an array of dominos - c

Here are the program specs:
This assignment will be to add and remove dominos from an array of dominos. Your structure will be a domino. It will hold 3 fields (used, number1 and number2).
The used field will either hold Y or N. If used is a Y, then number1 and number 2 are the two numbers for the domino. Your main function will only allocate the array
of dominos and read the option for which action to take. Actions are print, add, remove and quit (exits the program).
You must write at least the following four functions.
int getIndex();
void addDomino(Domino * d, int size);
void removeDomino(Domino * d, int offset);
void print(Domino * d, int size);
The getIndex function will get the index to be removed.
The addDomino function will find a free spot, and then read in number1 and number2 for that domino. It will also set that domino as being used (used = ā€˜Yā€™).
The removeDomino function will remove the domino at the location index. This can be done by setting the used flag for that index to ā€™Nā€™);
Problem: I am not getting the removeDomino function to work properly. It only removes one element and wont work again.
Problem: I am not getting the addDomino function to write over the removed domino index and then continue as normal. My function will only add to the end of the array.
Please Help.
Here is the program I wrote:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int number1;
int number2;
char used;
}domino;
int getIndex();
void addDomino(domino * d, int size);
void removeDomino(domino * d, int offset);
void print(domino * d, int size);
int main()
{
int size = 0;
int index = 0;
int offset = 100;
int counter = 0;
int i = 0;
char * dummy = malloc(sizeof(char));
printf("Enter the max number of dominoes: ");
scanf("%d%c", &size, dummy);
domino * d = malloc(sizeof(domino) * size);
domino current[size];
char * input = malloc(sizeof(char));
(*input) = ' ';
while((*input) != 'q')
{
printf("Enter (p) to print, (a) to add, (r) to remove, and (q) to quit: ");
scanf("%c%c", input, dummy);
switch(*input)
{
case 'p':
index = counter;
printf("Printing Dominoes\n");
for(index = 0; index < counter; index++)
{
if(current[index].used == 'y')
{
print(&current[index], index);
}
else
{
}
}
break;
case 'a':
addDomino(&current[index], index);
index++;
counter = index;
break;
case 'r':
getIndex();
removeDomino(&current[offset], offset);
break;
case 'q':
printf("All done\n");
break;
default:
break;
}
}
return 0;
}
int getIndex()
{
int offset = 0;
char dummy = ' ';
printf("Enter the index:");
scanf("%d%c", &offset, &dummy);
return offset;
}
void addDomino(domino * d, int size)
{
char * dummy = malloc(sizeof(char));
printf("Enter number 1\n:");
scanf("%d%c", &d->number1, dummy);
printf("Enter number 2\n:");
scanf("%d%c", &d->number2, dummy);
d->used = 'y';
}
void removeDomino(domino * d, int offset)
{
d->used = 'n';
}
void print(domino * d, int index)
{
printf("Domino %d (%d,%d)\n", index, d->number1, d->number2);
}

In your 'r' case, you forgot to use the return value of GetIndex.
The offset used in getIndex is local to that function and doesn't change the offset variable in your main.
if you change getIndex(); to offset = getIndex(); I think it should work correctly.

Related

How to delete a value in a struct?

I am trying to delete a "user" from the struct on my code.
For example, this is the struct I have:
struct clients {
char name[30];
int year;
float amount;
};
And this is the two outputs I have:
Name: Victor
Birth Year: 1998
Amount: 1000.00
Name: John
Birth Year: 1996
Amount: 1500.00
What I want to do is to delete John and all his information from my struct.
I am trying to learn dynamic memmory allocation, so I used this command:
clt = (struct clients*) malloc(n * sizeof(struct clients));
In my case I wish to write a command on case 3 that delete john from the struct, but I really can't figure out how to do that, I guess maybe using realloc? But I didn't even get close to write something it would work.
Here is my entire code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct clients {
char name[30];
int year;
float amount;
};
int main() {
struct clients *clt;
int option, n, d = 0;
char name[30];
printf("Welcome, Choose an option to begin:");
do {
printf("\n1 - Create a new list;\n");
printf("2 - Find a person in the list;\n");
printf("3 - Delete a person from the list.\n");
printf("4 - End the program.\n");
scanf("%d", &option);
switch (option) {
case 1:
printf("\nType how many clients you want to register:");
scanf("%d", &n);
// allocating memory for n numbers of struct person
clt = (struct clients*) malloc(n * sizeof(struct clients));
for (int i = 0; i < n; i++) {
printf("Type the name and the birth year:");
// To access members of 1st struct person,
scanf("%s %d", (clt + i)->name, &(clt + i)->year);
printf("\nType the amount:");
scanf("%f", &(clt + i)->amount);
}
break;
case 2:
printf("Type the name you want to find:");
scanf("%s", name);
for (int i = 0; i < n; i++) {
if (strcmp(&clt[i].name, name) == 0) {
printf("\nThats the name:");
printf("\nName: %s\n", (clt + i)->name);
printf("Birth Year: %d\n", (clt + i)->year);
printf("Amount: %.2f\n", (clt + i)->amount);
d++;
}
}
if (d == 0)
printf("This name doesn't exist\n");
break;
case 3:
break;
case 4:
break;
}
} while (option != 4);
return 0;
}
You need to discard the element from its location in the list by moving the contents in the remainder of the list up , and overwriting the target element.
for(int i = 0; i < n; i++) {
if (strcmp(&clt[i].name, name)==0)
{
printf("\nThats the name:");
printf("\nName: %s\n", (clt+i)->name);
printf("Birth Year: %d\n", (clt+i)->year);
printf("Amount: %.2f\n", (clt+i)->amount);
d++; // This seems to be your sentinel value?
}
if ( 1 == d && i+1 != n ) // if the sentinel is set, but we're not off the end of the list yet
{
memcpy(clt+i, clt+i+1, sizeof( struct clients ));
}
}
n--; // reduce the list length
You could do this in one memmove call if you prefer. As #Neil says in the comments, memmove is tolerant of overlapping source and destination.
if ( 1 == d && i != n-1 ) // if the sentinel is set, but we're not off the end of the list yet
{
memmove(clt+i, clt+i+1, (n-i-1) * sizeof( struct clients ));
break; // break for i
}
You are describing almost a dynamic array such as C++'s std::vector. It is static, but it could be a dynamic array without that much extra work. You could create a container, and split n into the container's capacity and size.
struct clients_array {
size_t size, capacity;
struct clients *data;
};
Instead of having multiple pointers, one could create a struct clients_array for each list. Sample code to deal with the struct clients_array.
#include <inttypes.h>
#include <assert.h>
#include <errno.h>
/** `a` constructor with `n` spots. Returns true or false depending on whether
it could be constructed. */
static int clients_array_reserve(struct clients_array *const a,
const size_t n) {
assert(a && n);
if(n > SIZE_MAX / sizeof *a->data) return errno = ERANGE, 0;
if(!(a->data = malloc(sizeof *a->data * n))) return 0;
a->capacity = n;
a->size = 0;
return 1;
}
/** `a` destructor; call this when `a` has been successfully constructed. */
static void clients_array_(struct clients_array *const a)
{ if(a) { free(a->data); a->data = 0; a->capacity = a->size = 0; } }
/** Extract a new uninitialized entry out of constructed `a`. Returns null if
there is no more space. */
static struct clients *clients_array_new(struct clients_array *const a) {
assert(a);
return a->capacity > a->size ? a->data + a->size++ : 0;
}
/** Remove index `rm` from `a`. */
static void clients_array_delete(struct clients_array *const a,
const size_t rm) {
assert(a && rm < a->size);
memmove(a->data + rm, a->data + rm + 1,
sizeof *a->data * (a->size - rm - 1));
a->size--;
}
Then one can have different, independent arrays at once. If you want to update the code, say to allow realloc to increase the size of the object, you only need to modify the container.

Segmentation fault in C? Arrays, pointers, functions

Hi due to my lack of knowledge in C (second year in college). Compiler ate my code and built the app. But after accepting first value - numOfIntegers it stops working and debugging tells that the segmentation has been failed. SIGSEGV.
How to fix that?
There is the code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* -----------------------------------------
Program: Question 1
Author: Maggot #9
Email: maggot99999#gmail.com
ID: B00076450
Date: 16 September 2015
Purpose: Who knows?
------------------------------------------ */
void wait(int);
void controlMenu(int, int[]);
int sumOfIntegers(int, int[]);
int avgOfIntegers(int, int[]);
int prodOfIntegers(int, int[]);
int minInteger(int, int[]);
int maxInteger(int, int[]);
const char * getName (int value)
{
static char * arrayName[] = {"first","second","third", "fourth",
"fifth","sixth", "seventh", "eighth", "ninth", "tenth"};
static char badValue[] = "unknown";
if (value<10 && value>=0)
return arrayName[value];
else
return badValue;
}
int getValue(int numOfInteger)
{
int value;
wait(100);
printf("Please enter %s the value:", getName(numOfInteger));
scanf("%d",&value);
return value;
}
void prepare(int * numOfIntegers)
{
wait(300);
printf("Hey again that C stupid lang\n\n");
wait(200);
printf("Please enter how many values you want to put: ");
scanf("%d",numOfIntegers);
return;
}
void initialize(int numOfIntegers,int* arrayNum[])
{
int i;
for(i=0; i<(numOfIntegers); i++)
arrayNum[i] = getValue(i);
wait(500);
printf("\nPlease enter press any button to continue");
wait(100);
getch();
wait(600);
system("cls");
wait(200);
return;
}
int main()
{
int numOfIntegers;
prepare(&numOfIntegers);
int arrayNum[numOfIntegers];
initialize(numOfIntegers, &arrayNum[numOfIntegers]);
controlMenu(numOfIntegers, &arrayNum[numOfIntegers]);
return 0;
}
void controlMenu(int numOfIntegers, int arrayNum[])
{
int i;
char chooseNum;
printf("Please choose any of the following:\n\n1. The integers accepted\n2. The sum of the integers\n3. The average of the integers\n4. The product of the integers\n5. The smallest integer\n6. The largest integer\n0. Exit menu\n");
while(1)
{
chooseNum = getch();
switch(chooseNum)
{
case '0':
return;
case '1':
printf("\n>>> The integers are:");
for(i=0; i<(numOfIntegers); i++)
{
printf("\n>>> The %s is %d", getName((i+1)), arrayNum[i]);
}
break;
case '2':
printf("\n>>> The sum of integers is: %d", sumOfIntegers(numOfIntegers, &arrayNum[numOfIntegers]));
break;
case '3':
printf("\n>>> The average of integers is: %d", avgOfIntegers(numOfIntegers, &arrayNum[numOfIntegers]));
break;
case '4':
printf("\n>>> The product of integers is: %d", prodOfIntegers(numOfIntegers, &arrayNum[numOfIntegers]));
break;
case '5':
printf("\n>>> The smallest integer is: %d", minInteger(numOfIntegers, &arrayNum[numOfIntegers]));
break;
case '6':
printf("\n>>> The largest integer is: %d", maxInteger(numOfIntegers, &arrayNum[numOfIntegers]));
break;
default:
break;
}
printf("\n\n");
}
}
int sumOfIntegers(int numOfIntegers,int arrayNum[])
{
int sum=0;
for(int i=0; i<(numOfIntegers); i++)
sum += arrayNum[i];
return sum;
}
int avgOfIntegers(int numOfIntegers, int arrayNum[])
{
int average=0;
average = sumOfIntegers(numOfIntegers, arrayNum[numOfIntegers])/numOfIntegers;
return average;
}
int prodOfIntegers(int numOfIntegers, int arrayNum[])
{
int i,product=0;
for(i=0; i<(numOfIntegers); i++)
product *= arrayNum[i];
return product;
}
int minInteger(int numOfIntegers, int arrayNum[])
{
int i,smallest=0;
smallest = arrayNum[0];
for(i=1; i<(numOfIntegers); i++)
{
if(smallest>arrayNum[i])
smallest=arrayNum[i];
else
continue;
}
return smallest;
}
int maxInteger(int numOfIntegers, int arrayNum[])
{
int i,largest=0;
largest = arrayNum[0];
for(i=1; i<(numOfIntegers); i++)
{
if(largest<arrayNum[i])
largest=arrayNum[i];
else
continue;
}
return largest;
}
void wait(int ms)
{
Sleep(ms);
return;
}
I can see this fault in getName() which will access memory beyond the array bounds
if (value>10 || value<1)
return arrayName[value];
I believe you are using the wrong test, try
if (value <= 10 && value > 0)
return arrayName[value-1];
assuming value is in the range 1..10 as the textual array implies.
2) a fault in GetValue where you input into numOfInteger but return value, which is uninitialised.
3) in prepare the statement
scanf("%d",&numOfIntegers);
will not pass the input value back to the caller. You should have either passed a pointer to the variable, or returned the value input.
But there might be a lot else wrong. Build your program step by step, checking and trying to break it as you go (with absurd input). Pay attention to compiler warnings - the second fault I listed will generate one.
EDIT okay... let's examine function prepare which after removing noise is
void prepare(int numOfIntegers)
{
scanf("%d",&numOfIntegers);
return;
}
This inputs a value to the function parameter that was passed. This is legal, since you can use a function argument in the same way you can a local variable (perhaps subject to const qualification).
Although it's not a coding error, it does not achieve anything. 1) you usually pass an argument like this to be used by the function in some way, perhaps in its limits and/or in its prompt. 2) Altering the argument like this will not find its way back to the caller.
Here are two ways to deal with this.
A) the function returns the input value
int prepare(void)
{
int value;
scanf("%d", &value); // the address of value
return value;
}
...
int input = prepare();
printf("%d\n", input);
B) the function takes a pointer argument
void prepare(int *value)
{
scanf("%d", value); // value is already a pointer
}
...
int input;
prepare(&input);
printf("%d\n", input);

C - Displaying the parts in ascending order by part number

I'm new here, so I have no idea if I posted this correctly. I did everything that the instructor told us to do for this program, but this last one has me stumped because we never talked about sorting in class. It says, "Modify the print() function so that it displays the parts sorted in ascending order by part number." I tried looking through the book and on the internet, but I just caused myself to become even more confused. Can anyone help me? Here's my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 25
#define MAX_PARTS 100
struct part {
int number;
char name[NAME_LEN + 1];
int on_hand;
float price;
};
int find_part(int number, const struct part inv[], int np);
void insert(struct part inv[], int *np);
void search(const struct part inv[], int np);
void update(struct part inv[], int np);
void print(const struct part inv[], int np);
int read_line(char [], int);
/**************************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**************************************************************/
int main(void)
{
char code;
struct part inventory[MAX_PARTS];
int num_parts = 0;
for (;;)
{
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != '\n') /* skips to end of line */
{
;
}
switch (code)
{
case 'i':
insert(inventory, &num_parts);
break;
case 's':
search(inventory, num_parts);
break;
case 'u':
update(inventory, num_parts);
break;
case 'p':
print(inventory, num_parts);
break;
case 'q':
return 0;
default:
printf("Illegal code\n");
break;
}
printf("\n");
}
}
/************************************************************
* find_part: Looks up a part number in the inv array. *
* Returns the array index if the part number *
* is found; otherwise, returns -1. *
************************************************************/
int find_part(int number, const struct part inv[], int np)
{
int i;
for (i = 0; i < np; i++)
{
if (inv[i].number == number)
{
return i;
}
}
return -1;
}
/****************************************************************
* insert: Prompts the user for information about a new *
* part and then inserts the part into the inv *
* array. Prints an error message and returns *
* prematurely if the part already exists or the *
* array is full. *
****************************************************************/
void insert(struct part inv[], int *np)
{
int part_number;
if (*np == MAX_PARTS)
{
printf("Database is full; can't add more parts.\n");
return;
}
printf("Enter part number: ");
scanf("%d", &part_number);
if (find_part(part_number, inv, *np) >= 0)
{
printf("Part already exists.\n");
return;
}
inv[*np].number = part_number;
printf("Enter part name: ");
read_line(inv[*np].name, NAME_LEN);
printf("Enter quantity on hand: ");
scanf("%d", &inv[*np].on_hand);
printf("Enter the price of the item: ");
scanf("%f", &inv[*np].price);
(*np)++;
}
/************************************************************
* search: Prompts the user to enter a part number, then *
* looks up the part in the inv array. If the *
* part exists, prints the name and quantity on *
* hand; if not, prints an error message. *
************************************************************/
void search(const struct part inv[], int np)
{
int i, number;
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number, inv, np);
if (i >= 0)
{
printf("Part name: %s\n", inv[i].name);
printf("Quantity on hand: %d\n", inv[i].on_hand);
printf("Item Price: %.2f\n", inv[i].price);
}
else
{
printf("Part not found.\n");
}
}
/*************************************************************
* update: Prompts the user to enter a part number. *
* Prints an error message if the part can't be *
* found in the inv array; otherwise, prompts the *
* user to enter change in quantity on hand and *
* updates the array. *
*************************************************************/
void update(struct part inv[], int np)
{
int i, number, change, userChoice, changePartNum;
float changePrice;
char *changeName[] = {""};
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number, inv, np);
if (i >= 0)
{
printf("Enter your selection to edit this particular part:\n"
"\t\t Type 1 to change the Part Number\n"
"\t\t Type 2 to change the Part Name\n"
"\t\t Type 3 to change the Price\n"
"\t\t Type 4 to change the Quantity on Hand\n"
"\t\t Type 5 to exit without making changes\n\n"
"\t\t Enter your choice here: ");
scanf("%d", &userChoice);
switch ( userChoice )
{
//printf("Would you like to change the Part Number? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 1:
printf("Enter new part number: ");
scanf("%d", &changePartNum);
inv[i].number = changePartNum;
printf("Change part num: %d\n", changePartNum);
printf("inv[i].number: %d\n", inv[i].number);
break;
//printf("Would you like to change the Part Name? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 2:
printf("Enter new name of part: ");
scanf("%s", changeName);
printf("Change part name: %s\n", changeName);
//strcpy (*changeName, inv[i].name[NAME_LEN + 1]);
//printf("&inv[i].name[NAME_LEN + 1]: %d\n", &inv[i].name[NAME_LEN + 1]);
break;
//printf("Would you like to change the price? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 3:
printf("Enter change in item price: ");
scanf("%f", &changePrice);
inv[i].price = changePrice;
break;
//printf("Would you like to change the quantity on hand? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 4:
printf("Enter change in quantity on hand: ");
scanf("%d", &change);
inv[i].on_hand = change;
break;
case 5:
printf("Exiting the editor.");
break;
default:
printf("Your choice is not on the list.");
break;
}
}
else
{
printf("Part not found.\n");
}
}
/************************************************************
* print: Prints a listing of all parts in the inv array, *
* showing the part number, part name, and *
* quantity on hand. Parts are printed in the *
* order in which they were entered into the *
* array. * *
************************************************************/
void print(const struct part inv[], int np)
{
int i;
printf("Part Number Part Name "
"Quantity on Hand "
" Price\n");
for (i = 0; i < np; i++)
{
printf("%7d\t\t %-5s%31d\t%.2f\n", inv[i].number,
inv[i].name, inv[i].on_hand, inv[i].price);
}
}
/*************************************************************
* read_line: Skips leading white-space characters, then *
* reads the remainder of the input line and *
* stores it in str. Truncates the line if its *
* length exceeds n. Returns the number of *
* characters stored. *
*************************************************************/
int read_line(char str[], int n)
{
int ch = 0;
int i = 0;
while (isspace (ch = getchar()))
{
;
}
while (ch != '\n' && ch != EOF)
{
if (i < n)
{
str[i++] = ch;
}
ch = getchar();
}
str[i] = '\0';
return i;
}
If your professor allows you to use a function from the standard libary, check out C library function to do sort, which provides an answer that shows you how to use qsort(). If you're on linux, you can also get documentation on the function (and other standard library functions) by doing man qsort. If you're not on linux, check out any of the various online man pages.
If not, your professor probably expects you to do your own research. Generally the bubble sort algorithm is taught to beginners because of its simplicity. rosettacode provides pseudo-code on what a bubble sort should look like:
repeat
hasChanged := false
decrement itemCount
repeat with index from 1 to itemCount
if (item at index) > (item at (index + 1))
swap (item at index) with (item at (index + 1))
hasChanged := true
until hasChanged = false
Note that in your print function, you already have all you need, the array and its length. You demonstrated this by looping through it and printing out all the member variables. Now you just need to write the algorithm for the sort. One thing you need for the algorithm is the comparator function. You stated that you need to sort by part number. That means your comparator function will probably look like this:
int compare(struct part a, struct part b)
{
return (a.number < b.number);
}
For qsort():
qsort(inv, np, sizeof(part), compare);
In your sorting algorithm (that you should write yourself):
if (item.number at index) > (item.number at (index + 1))

Advance calculator for finding the mode of a set of numbers

I am currently working on a project for school in which I need to program a calculator to determine the mode of a set of numbers. The parameters are the numbers have to be between 1 and 30. Have to check whether the user inserts a number within that range and that the number must be validated as an integer. I have most of it done except my main issues are the for loop in inputing the numbers and validating them and making sure my mode function works. Any suggestions in fixing the issue with the loop? Also I must use a mode function in order to calculate the mode does the one I'm using work well or is there a better way in going about it?
#include <stdio.h>
#include <string.h>
#include <math.h>
int mode(int *num, int size);
int main(int n, char **p) {
int modearray[], size, i;
printf("What is the size of the Array?");
scanf("%d", &size);
for (i=0; i<modearray[size]; i++) {
printf("Enter an integer value (1 to 30): ");
scanf("%d", modearray[i]);
if (modearray[i] < 1 || modearray[i] > 30) {
printf("Please enter a value within the range");
scanf("%d", modearray[i])
}
else if (sscanf(p[i], "%i", &a[i]) != 1) {
printf("ERROR\n");
return -1;
}
}
}
//used the mode function code frome http://www.dreamincode.net/forums/topic/43713- pointers-and-modefunction/
int mode(int *num, int size) {
int currentnum = (*num);
int count = 0;
int modenum = -1;
int modecount = 1;
for (int x=0; x<size; x++) {
if (currentnum==(*num + x)) count ++;
else {
if(count > modecount) {
modenum = currentnum;
// modecount = count;
x--;
}
currentnum=*(num + x);
count = 0;
}
}
}
As Charlie and user2533527 have already indicated, there are errors in the OP code, and they have offered suggestions regarding those errors. There are a few others that I have noted in my edit of your original code below, that without addressing, the code did not build and/or run. So, if you are interested, look at the inline comments at the bottom of this post to see some corrections to your original code.
This answer is focused on validation of input, per your stated objective ( Have to check whether the user inserts a number within that range and that the number must be validated as an integer ) Specifically it appears you need to verify that the numbers input fall within a range, AND that they all be an integers.
If you move all of the validation steps into one function, such as:
int ValidateInput(char *num)
{
if(strstr(num, ".")!=NULL) return FLOAT;
if (atoi(num) < 1) return SMALL;
if (atoi(num) > 30) return LARGE;
return VALID;
}
then the main user input loop can be easily executed to include specific errors, if any, or continue with data collection by using a switch() statement, such as:
status = ValidateInput(number);
switch(status) {
case VALID:
modearray[i] = atoi(number);
printf("Enter an integer value %d: (1 to 30): ", i+2);
break;
case FLOAT:
printf("float detected, enter an integer");
i--;//try again
break;
case SMALL:
printf("value too small, enter value from 1 to 30");
i--;//try again
break;
case LARGE:
printf("value too large, enter value from 1 to 30");
i--;//try again
break;
default:
//do something else here
break;
}
Altogether, this approach does not use the mode function, rather replaces it with ValidateInput() which ensures only numbers that are integers, and within the stated range are included in the modearray varible.
EDIT to include searching for mode (highest occurring number within group)
My approach will do three things to get mode
sort the array,
walk through the sorted array tracking count of the matches along the way.
keep the highest string of matches.
To do this, I will use qsort() and looping in the mode() function.
int mode(int *num, int size) {
int count = 0;
int countKeep=0;
int modenum = -1;
qsort(num, size, sizeof(int), cmpfunc);
//now we have size in ascending order, get count of most occuring
for (int x=1; x<size; x++)
{
if(num[x-1] == num[x])
{
count++;
if(count > countKeep)
{
countKeep = count;
modenum=num[x];
}
else
{
count = 0;
}
}
}
return modenum;
}
Here is the complete code for my approach: (This code will capture the mode of a string of numbers with only one mode. You can modify the looping to determine if the string is multi-modal, or having two equally occuring numbers)
#include <ansi_c.h> //malloc
//#include <stdio.h>//I did not need these others, you might
//#include <string.h>
//#include <math.h>
int ValidateInput(char *num);
int mode(int *num, int size);
int cmpfunc (const void * a, const void * b);
enum {
VALID,
FLOAT,
SMALL,
LARGE
};
int main(int n, char **p)
{
int *modearray, size, i;
int *a;
char number[10];
int status=-1;
int modeOfArray;
printf("What is the size of the Array?");
scanf("%d", &size);
modearray = malloc(size*sizeof(int));
a = malloc(size);
printf("Enter an integer value 1: (1 to 30): ");
for (i=0; i<size; i++)
{
scanf("%s", number);
//Validate Number:
status = ValidateInput(number);
switch(status) {
case VALID:
modearray[i] = atoi(number);
printf("Enter an integer value %d: (1 to 30): ", i+2);
break;
case FLOAT:
printf("float detected, enter an integer");
i--;//try again
break;
case SMALL:
printf("value too small, enter value from 1 to 30");
i--;//try again
break;
case LARGE:
printf("value too large, enter value from 1 to 30");
i--;//try again
break;
default:
//do something else here
break;
}
}
modeOfArray = mode(modearray, size);
getchar();//to view printf before execution exits
}
int ValidateInput(char *num)
{
if(strstr(num, ".")!=NULL) return FLOAT;
if (atoi(num) < 1) return SMALL;
if (atoi(num) > 30) return LARGE;
return VALID;
}
int mode(int *num, int size) {
int count = 0;
int countKeep=0;
int modenum = -1;
qsort(num, size, sizeof(int), cmpfunc);
//now we have size in ascending order, get count of most occuring
for (int x=1; x<size; x++)
{
if(num[x-1] == num[x])
{
count++;
if(count > countKeep)
{
countKeep = count;
modenum=num[x];
}
else
{
count = 0;
}
}
}
return modenum;
}
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
Assuming that the question is about crashing after the scanf in to array:
int main(int n, char **p) {
int *modearray, size, i;
printf("What is the size of the Array?");
scanf("%d", &size);
modearray = malloc(size * sizeof(int)); //imo size of int is 4 so u can replace with
for (i=0; i<modearray[size]; i++) {
printf("Enter an integer value (1 to 30): ");
scanf("%d", modearray[i]);
if (modearray[i] < 1 || modearray[i] > 30) {
printf("Please enter a value within the range");
scanf("%d", &modearray[i])
}
else if (sscanf(p[i], "%i", &a[i]) != 1) {
printf("ERROR\n");
return -1;
}
}
}

Segmentation fault with no pointers?

Here is my code and problem. The code compiles fine. But when I run it. After I enter the menu option in getMenuOption() "Segmentation Fault (core dumped)" pops up. What is wrong?
I'm new to programming in general. Thanks for the help if its provided.
#include <stdio.h>
#include<math.h>
#define CALCULATE_PI 'a'
#define CALCULATE_GEOMEAN 'b'
#define CALCULATE_HARMMEAN 'c'
void printInstructions (void);
void printMenuOptions (void);
int runMenuOption ();
int getMenuOption ();
int getLimit ();
int calculatePi ();
int calculateGeoMean ();
int calculateHarmonicMean ();
int main(void)
{
printInstructions();
printMenuOptions();
runMenuOption(getMenuOption());
return 0;
}
void printInstructions (void)
{
printf("======================================================\n");
printf("= PI, Geometric Mean, and Harmonic Mean Calculator =\n");
printf("= Please refer to the menu to choose calucaltion =\n");
printf("=Choose desired menu option and press enter to begin =\n");
printf("= Proceed to follow on-screen instructions =\n");
printf("======================================================\n\n\n");
return;
}
void printMenuOptions (void)
{
printf("3 choices: Please enter a VALID letter.\n");
printf("Choice 'a' = Calcualtes PI\n");
printf("Choice 'b' = Calculates Geometric Mean\n");
printf("Choice 'c' = Calculates Harmonic Mean\n\n");
return;
}
int runMenuOption (int getMenuOption())
{
char option;
double answer,
Pi = 0.0,
geoMean = 0.0;
option = getMenuOption();
switch (option)
{
case CALCULATE_PI:
calculatePi(getLimit());
answer = Pi;
break;
case CALCULATE_GEOMEAN:
calculateGeoMean(getLimit());
answer = geoMean;
case CALCULATE_HARMMEAN:
printf("Harmonic Mean");
break;
default:
printf("Incorrect Character!\n");
printf("Try again");
break;
}
printf("Your answer is %5p", &answer);
return 0;
}
int getMenuOption (void)
{
char option;
printf("Please enter choice: ");
scanf("%c", &option);
return option;
}
int getLimit ()
{
int limit;
scanf("%d", &limit);
return limit;
}
int calculatePi (void)
{
int limit,
count = 0,
Pi = 0;
printf("Please enter the PI limit: ");
limit = getLimit();
for (count = 1; count <= limit; count++)
{
Pi += 1 / count;
}
return sqrt(Pi * 6);
}
int calculateGeoMean()
{
int limit,
userValue = 0,
count = 0;
double geoMean = 0;
limit = getLimit();
while(count <= limit)
{
if (userValue <= 0)
printf("Incorrect. Try again");
else
{
count++;
userValue *= userValue;
}
}
geoMean = userValue;
return sqrt(userValue);
}
int calculateHarmonicMean()
{
int limit,
userValue = 0,
count = 0;
double harmMean = 0;
limit = getLimit();
while(count <= limit)
{
if (userValue <= 0)
printf("Incorrect. Try again");
else
{
count++;
userValue *= 1 / userValue;
}
}
harmMean = userValue;
return limit / userValue;
}
This function definition is totally wrong.
int runMenuOption (int getMenuOption())
either you can pass the return value of getMenuOption like this
int runMenuOption (int option)
or
you shouldn't pass any value to this function and call getMenuOption inside runMenuOption. You are doing both, which is incorrect.
int runMenuOption (int getMenuOption())
Here's your problem.
That should be:
int runMenuOption (int opt)
Also, you shouldn't be calling getMenuOption() within runMenuOption since you're calling getMenuOption() as you pass it to runMenuOption as a parameter. runMenuOption should only have a switch statement.
You need to modify the definition of your function from int runMenuOption (int getMenuOption()) to int runMenuOption (int option). In the call, getMenuOption() will be invoked and the output placed into the stack frame of the called function.
According to your declaration of the runMenuOption function, it takes a pointer to a function which returns an integer as its first argument:
int runMenuOption (int getMenuOption())
The function is then called in this line:
option = getMenuOption();
This is perfectly fine. However, the problem lies in this line:
runMenuOption(getMenuOption());
Here you are calling the getMenuOption function and passing the return value into the runMenuOption function. But what you should be doing is passing the function itself as the argument:
runMenuOption(getMenuOption);
The reason you are getting a Segmentation Fault error is because the return value from the getMenuOption function is being treated as a function pointer, and your program is attempting to call a function at that address, which is of course invalid.

Resources