I made this simple program that asks the user to input the number of columns the matrix called arp is going to have because, that way when the program asks the user to input a number so it can find matches on the numbers stored at the array without comparing all 10 columns allocated on memory with array to pointer type.
The problem here comes when the user inputs into the columns size definition 2. All works fine before the last function of p3() does its job, then it doesn't even return to main to execute the infinite loop defined there. I have tried removing the pointers and didn't work; I also tried removing other parts of the code but still nothing...
Update: Tried removing the function to find elements felmnt() and still the same.
Here is The Buggy Code:
#include <stdio.h>
#include <stdlib.h>
int loop = 1;
void keepalive(void)
{
int ckr = 0;
fflush(stdin);
printf("\n\n ******[s]<< CONTINUE | EXIT >>[n]******\n");
while(printf(" > ") && (ckr = getchar()) != 's' && ckr != 'n') fflush(stdin);
getchar();
if(ckr == 'n') loop = 0;
system("CLS");
}
void felmnt(int *colu, int (*arp)[10])
{
int nius=0, colmts, x, i, ejct;
do
{ // loop to let the user find more elements inside matrix
colmts=0;
printf("\n Insert The Number To Find\n > ");
scanf("%d", &nius);
for(x=0; x<*colu; x++) // search of element inside matrix
{
for(i=0; i<=9; i++)
if(nius == arp[i][x])
colmts++;
}
if(colmts>1) // results printing
{
printf("\n %d Elements Found", colmts);
}else if(colmts)
{
printf("\n 1 Element Found");
}else
{
printf("\n Not Found");
}
printf("\n TRY AGAIN? s/n\n > ");
ejct=getchar();
getchar();
}while(ejct=='s');
}
void mat(int *colu, int (*arp)[10])
{
int ci, cn, tst=0;
for(ci=0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++)
{
printf("\n Input The Number [%d][%d]\n > ", ci+1, cn+1);
scanf(" %d", &arp[cn][ci]);
}
}
printf(" Numbers Inside Matrix> ");
for(ci = 0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++) printf(" %d", arp[cn][ci]);
}
}
void p3(void)
{ // >>>>main<<<<
int colu=0;
while(loop)
{
printf("\n Input The Quantity Of Columns To Use\n > ");
scanf("%d", &colu);
int arp[10][colu];
mat(&colu, arp);
felmnt(&colu, arp);
keepalive();
}
}
int main(void)
{
int ck = 0, ndx;
while(1)
{ // infinite loop
p3();
fflush(stdin);
printf("\nPause !!!");
getchar();
}
return 0;
}
Related
I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.
I want to use a function to scanf up to 10 values for an array with the size 10, and also keep track of the number of values that are in the array because I'll need it later for solving some maths about the array, (max value, min value, etc.).
#include <stdio.h>
int enter(int MeasurmentData[], int nrOfmeasurments)
{
for(int i=0;i<10;++i)
{
int MeasurmentData[10];
scanf("%d",&MeasurmentData[i]);
int nrOfmeasurments = 0;
nrOfmeasurments ++;
return nrOfmeasurments;
}
int main()
{
int MeasurmentData[10];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n");
printf("v (View)\n");
printf("e (Enter)\n");
printf("c (Compute)\n");
printf("r (Reset)\n");
printf("q (Quit)\n");
printf("enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') \\ enter values
{
int MeasurmentData[10];
int nrOfmeasurments;
enter(MeasurmentData, nrOfmeasurments);
}
else if(menuoption == 'v') \\\ view values
{
//printf("%d", MeasurmentData[]);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
return 0;
}
}
}
When I run the program it should print Measurment tool 2.0, after the the user has the choice of inputting e(enter) which will scan in up to 10 values into an array, if the user clicks q(quit) while in the enter option already he will be returned to the main menu where he can do whatever.
V(view) prints out the array for the user so that he can view what elements are inside.
C(compute) uses the elements inside and the nr of elements to calculate the highest value element, lowest.
There are some errors in your code. Ill try to explain. You have over declared your variables too many times. And since you have a fixed loop you don't need to count the measurements you will always read 10 measurements.
Below are the code with some modifications. Feel free to ask anything about it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXIMUM_MEASURMENT 10
int enter(int MeasurmentData[])
{
char input[100];
int nrMeasurement = 0;
// reseting Measurment data
for(int i=0;i<MAXIMUM_MEASURMENT;++i) MeasurmentData[i] = 0;
for(int i=0;i<MAXIMUM_MEASURMENT;++i)
{
scanf("%99s", input);
if(strcmp(input, "q") == 0) {
break;
}
MeasurmentData[i] = (int) strtol(input, (char **)NULL, 10);
nrMeasurement++;
}
return nrMeasurement;
}
void showMeasurments(int* MeasurmentData, int length) {
int i = 0;
printf(" ======== Measurment ======== \n");
for(i = 0; i < length; i++) {
printf("%d ", MeasurmentData[i]);
}
printf("\n");
}
int main()
{
int MeasurmentData[MAXIMUM_MEASURMENT];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n" "v (View)\n" "e (Enter)\n" "c (Compute)\n" "r (Reset)\n" "q (Quit)\n enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') // enter values
{
enter(MeasurmentData);
}
else if(menuoption == 'v') // view values
{
// show
showMeasurments(MeasurmentData, MAXIMUM_MEASURMENT);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
break;
}
}
return 0;
}
Edit: i have updated the code. So i have read the comments of your question and there you have explained a little better what you are trying to accomplish. So since you have the requirement to press 'q' to stop reading values. I have to read all measurments as string and convert to integer if it is not the character q.
Edit 2: Thanks to #user3629249 to point out some of the flaws from the code ill update with his suggestions.
I am trying to copy the array winner from my function 'enter', so that i am able to just output it on the 'previous' function. When picking the option for the previous option I have gotten nothing outputting. Its only the last function named 'previous' that is not working, but to produce the problem the majority of the code is needed.
#include <stdio.h>
#include <string.h>
char enter(char names[][20]);
void menu();
void previous(char winner[][8]);
int main()
{
char names[16][20];
int i;
printf("Please enter the names of the players:\n");
/*Making the user enter 16 times*/
for (i = 0; i < 16; i++)
{
scanf("%9s", &names[i]);
fflush(stdin);
}
/*Clearing Screen*/
system("cls");
menu(names);
return names[16][20];
}
void menu(char names[][20], char winner[][8])
{
int choice;
printf("Please select one of the following options:\n\n"
"Press 1 to enter game results\n"
"Press 2 to display the current round\n"
"Press 3 to display the players advancing to the next round\n"
"Press 4 to display the previous round\n"
"Press 5 to exit the program\n");
scanf("%d", &choice);
if(choice == 1)
{
enter(names);
}
system("cls");
if(choice == 3)
{
previous(winner);
}
}
char enter(char names[][20])
{
int result;
int score1;
int score2;
int p, c, j, l, i;
char winner[8][8];
system("cls");
for(i = 0; i < 8; i++)
{
printf("\n\n%s vs %s",names[i],names[i+8]);
score1 = 0;
score2 = 0;
for(j = 0; j < 5; j++)
{
printf("\n\nEnter game %d results, press 1 if %s won or"
" 2 if %s won :\n",(j+1), names[i], names[i+8]);
scanf("%d", &result);
if(result == 1)
{
score1++;
}
if(result == 2)
{
score2++;
}
printf("\n\n1Current score is %d-%d", score1, score2);
if(score1 == 3)
{
printf("\n\n%s adavances to the next round!",names[i]);
strncpy(winner[i], names[i], 10);
printf("\n\nPress Enter to Continue");
getch();
system("cls");
break;
}
if(score2 == 3)
{
printf("\n\n%s adavances to the next round!",names[i+8]);
strncpy(winner[i], names[i+8], 10);
printf("\n\nPress Enter to Continue");
getch();
system("cls");
break;
}
}
}
system("cls");
printf("The players advancing to the next round are:\n\n");
for(p = 0; p < 8; p++)
{
for(c = 0; c < 8; c++)
{
printf("%c",winner[p][c]);
}
printf("\n");
}
printf("\n\nPress Enter to Continue");
getch();
system("cls");
menu(names, winner);
return winner[8][8];
}
void previous(char winner[][8])
{
int i, j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
printf("%c",winner[i][j]);
}
printf("\n");
}
}
There is no data for the array winner in your program! At least not when you call it for the first time.
The signature for the menu function is:
void menu(char names[][20], char winner[][8]);
but you call it from main like this:
menu(names);
The winner parameter is missing. This shouldn't happen, but you have declared a prototype for this function, namely:
void menu();
Unfortunately, C treats the empty parens as meaning "whatever parameters you pass", not as function that takes no parameters. That means that your function call slips by. The fix is to provide the correct signature for the prototype and also to pass a suitable winner array from main.
Strangely, your enter function provides a local array winner. This array will always be a new array when you call enter. That's probably not what you want. As is, your program should have one names and one winner array. (You can pass these arrays around, but you should make sure that tese arrays are consistent. Don't create new arrays when you really want to operate on existing ones.)
You also call your menu recursively. That means the you go ever deeper into the call structure without real benefit. Dont do that; use a loop instead: do display the menu while the user hasn't chosen "quit". (There are applications for recursive functions, but this isn't one.)
my program is supposed to implement a dfa for a binary string... dfa is a machine that can be in only one state at a time (when you enter a string the machine should use it and at the last step it should reach to the final state. if so, we say that string is accepted by the machine) ...
this machine works this way:
at first program asks the number of states, if u consider my pic, u see that it has 3 states(q0,q1,q2) so we enter 3. then it asks about the number of inputs. my inputs are 0,1 so I enter 2... then it asks to enter the inputs, we enter 0 then 1! then it asks the number of final states, the final state here is only q1... so we enter it... then u see this: (q0,0) = q it means q0 goes to which state with 0... for example here q0 goes to q0 with 0 ... others are like that. after filling this part, we enter the sring and it would say if string is valid or not
here is the code:
#include<stdio.h>
#include<conio.h>
int ninputs;
int check(char,int ); //function declaration
int dfa[10][10];
char c[10], string[10];
int main()
{
int nstates, nfinals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&nstates); // 3
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&ninputs); // 2
printf("\nenter input symbols\t");
for(i=0; i<ninputs; i++)
{
printf("\n\n %d input\t", i+1);
printf("%c",c[i]=getch()); // 01
}
printf("\n\nenter number of final states\t");
scanf("%d",&nfinals); // 1
for(i=0;i<nfinals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]); // 1
}
printf("-----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<ninputs; i++)
{
for(j=0; j<nstates; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
// q(0,0)=0
// q(1,0)=0
// q(2,0)=2
// q(0,1)=1
// q(1,1)=2
// q(2,1)=1
}
}
do
{
i=0;
printf("\n\nEnter Input String.. ");
scanf("%s",string); // 01
while(string[i]!='\0')
{
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<nfinals ;i++)
{
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
getch();
printf("\nDo you want to continue.? \n(y/n) ");
}
}
}
while(getch()=='y');
getch();
}
int check(char b,int d)
{
int j;
for(j=0; j<ninputs; j++)
if(b==c[j])
return(dfa[d][j]);
return -1;
}
the problem is that when I enter my string, the program says it's invalid, however it should be accepted by the machine... for example consider the machine in the photo and test this string on it: 01
so what's wrong with the code?
Optimized C code for Accepting String using DFA
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int a,b,i,j,k,state,ch;
char s[10][10],*st,v[10],ss[10];
printf("Enter the number of state:\n");
scanf("%d",&a);
printf("Enter State :\n");
for(i=0;i<a;i++)
{
fflush(stdin);
scanf("%c",&ss[i]);
}
printf("Enter th no. of var..:\n");
scanf("%d",&b);
printf("Enter variable :\n");
for(i=0;i<b;i++)
{
fflush(stdin);
scanf("%c",&v[i]);
}
printf("Enter table:\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
fflush(stdin);
scanf("%c",&s[i][j]);
}
}
printf("Enter string :\n");
fflush(stdin);
gets(st);
i=0;
state=0;
while(st[i]!='\0')
{
for(j=0;j<b;j++)
{
if(st[i]==v[j])
{
if(s[state][j]=='-')
{
goto check;
}
else
{
for(k=0;k<a;k++)
{
if(s[state][j]==ss[k])
{
printf("State:%c\n",s[state][j]);
state=k;
goto o;
}
}
}
o:
}
}
i++;
}
check:
ch=1;
for(i=0;i<b;i++)
{
if(s[state][i]!='-')
{
ch=0;
}
}
if(ch==1)
{
printf("String is matching..");
}
else
{
printf("String is not matching..");
}
getch();
return 0;
}
Just put an space before %c . And edit some lines of the code.
This code is successfully complied on Code::Blocks 13.12 version.
As it C++ Compiler save the file with .cpp extension.
And Click On the " output image " . You will see how to input & output from
console
<--------Here Is the edited code && It will work fine ---> #
output image
#include<stdio.h>
#include<cstdio>
#include<iostream>
int ninputs,bb;
int check(char,int ); //function declaration
int dfa[10][10];
char c[10], string[10],b;
int main()
{
int nstates, nfinals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&nstates);
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&ninputs);
printf("\nenter input symbols");
for(i=0; i<ninputs; )
{
bb =i;
printf("\n %d input",bb+1);
// printf(" %c",c[i]=getchar());
scanf(" %c" , &c[i]); //just put an space before %c
i++;
}
printf("\n\nenter number of final states\t");
scanf("%d",&nfinals);
for(i=0;i<nfinals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]);
}
printf("-----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<ninputs; i++)
{
for(j=0; j<nstates; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
}
}
do
{
i=0;
printf("\n\nEnter Input String.. ");
scanf("%s",string);
while(string[i]!='\0')
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<nfinals ;i++)
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
printf("\nDo you want to continue.? \n(y/n) ");
}
while(b =='y');
scanf(" %c", &b); // chnge here
}
int check(char b,int d)
{
int j;
for(j=0; j<ninputs; j++)
if(b==c[j])
return(dfa[d][j]);
return -1;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
main()
{
int state,symbol;
char str[30];
cout<<"Enter the string which you want to check :: "<<endl;
fgets(str,sizeof(str),stdin);
cout<<"Enter the Number of state :: ";cin>>state;
cout<<"Enter the Number of input symbol :: ";cin>>symbol;
cout<<"Enter the states : "<<endl;
char st[state];
for(int i=0;i<state;i++)
{
cout<<"state : "<<i<<" : ";cin>>st[i];
}
cout<<"Enter the input symbol : "<<endl;
char sy[symbol];
for(int i=0;i<symbol;i++)
{
cout<<"symbol : "<<i<<" : ";cin>>sy[i];
}
char table[state][symbol];
cout<<"------Enter next move or state------ if no relation put -"<<endl;
for(int i=0;i<state;i++)
{
for(int j=0;j<symbol;j++)
{
cout<<"("<<st[i]<<", "<<sy[j]<<") = ";cin>>table[i][j];
}
}
char ststate,fnstate;
cout<<"Start state :: ";cin>>ststate;
cout<<"Final state :: ";cin>>fnstate;
cout<<"----------------------------------"<<endl;
int str_len=strlen(str);
int p,q;
int flag=1;
int j=0;
for(j=0;j<str_len-1;j++)
{
cout<<"input state"<<endl;
for(int i=0;i<state;i++)
{
if(st[i]==ststate)
{
p=i;
cout<<p<<endl;
break;
}
else
{
p=-1;
}
}
cout<<"input string"<<endl;
for(int i=0;i<state;i++)
{
if(sy[i]==str[j])
{
q=i;
// cout<<q<<endl;
break;
}
else
{
q=-1;
}
}
if(p!=-1 && q!=-1)
{
cout<<"table output :: "<<table[p][q]<<endl;
ststate=table[p][q];
}
}
cout<<endl;
cout<<"----------------------------------------------------------------------------------";
if((table[p][q]==fnstate) && (j==str_len-1))
{
cout<<"String is recognized"<<endl;
flag=0;
}
if(flag!=0)
{
cout<<"String is not recognized.";
}
cout<<endl;
return 0;
}
This part of the code:
while(string[i]!='\0')
{
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<nfinals ;i++)
{
uses and changes i in both loops. You need to use something else in one of them.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Develop a program that will play the Lotto game. The program should allow a user to
enter their 6 selected numbers and give them a set of options, each performing a
specific requirement. You must store the 6 numbers in a 1-Dimensional array.
There are a number of requirements that your program must meet. Your program
must be modularised (i.e. use functions) and each task should be dealt in a separate
function. The program should display a simple menu to the user and each option in
the menu will be implemented by calling a separate function. You must use pointer
notation to access array elements - NOT subscripts
The requirements are as follows (each implemented in a separate function):
Read the 6 selected numbers from the keyboard. Perform any necessary
validation (error-checking) required (e.g. all numbers must be different, range
1-42, etc.). THIS IS THE PART WHICH I CANNOT DO
Display the contents of the 1-D array containing your lotto numbers that you
entered.
Sort the contents of the array in increasing order (i.e. 1st element = smallest
number, 2nd element = 2nd smallest number, etc.). You may use any sorting
algorithm of your choice.
Compare your chosen lotto numbers in the 1-D array with the following
winning numbers:
1,3,5,7,9,11 - 42 (Bonus number)
5 Display the frequency of the numbers entered each time the user enters a new AND I HAVE NO CLUE HOW TO EVEN START THIS
set of numbers (without exiting the program) on the screen. For example, it
might display:
number 1 has been selected x times
number 7 has been selected x times
number 28 has been selected x times
etc.,
6 Exit program
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include <stdlib.h>
#define NUMBERS 6
// Declare Prototype
void getnumbers(int*);
void displaynumbers(int*);
void sortnumbers(int*);
void comparenumbers(int*,int*);
void frequencynumbers(int*);
void exit();
main()
{
int choice=0;
int lotto_no[NUMBERS];
int winning_no[NUMBERS]={1,3,5,7,9,11};
do
{
system("cls");
printf("\n======================MAGIC Lotto======================");
printf("\n\n\n--------------Use 1-6 to navigate the menu-------------");
printf("\n\n\n1.Pick your 6 lucky numbers");
printf("\n\n2.Disply your numbers");
printf("\n\n3.Display your lucky numbers in increasing order");
printf("\n\n4.Compare your numbers to see your prize!");
printf("\n\n5.Frequency of the numbers entered each time");
printf("\n\n6.Exit");
printf("\n\n\n========================================================");
printf("\n");
scanf("%d",&choice);
system("cls");//Clears the menu from the screen while we selesc an option from the Menu
if (choice == 1)
{
getnumbers(lotto_no);
}
if (choice == 2)
{
displaynumbers(lotto_no);
}
if(choice == 3)
{
sortnumbers(lotto_no);
}
if(choice == 4)
{
comparenumbers(lotto_no,winning_no);
}
if(choice == 5)
{
frequencynumbers(lotto_no);
}
if(choice == 6)
{
exit();
}
flushall();
getchar();
}
while(choice>1 || choice<7);
}
void getnumbers(int *lotto_no)
{
int i;
printf("Enter your numbers\n");
for(i=0;i<NUMBERS;i++)
{
scanf("%d", &*(lotto_no+i) );
if ( *(lotto_no+i) >0 || *(lotto_no + i ) <= 43 )
{
continue;
}
else
{
printf(" Please enter a value in between 1 and 42");
scanf("%d", &(*(lotto_no+i)));
}
}
}
void displaynumbers(int *lotto_num)
{
int i;
printf("Your lucky numbers are as follow:\n");
for(i=0;i<NUMBERS;i++)
{
printf("%d ",*(lotto_num+i));
}
}
void sortnumbers(int *lotto_num)
{
int i;
int j;
int temp;
for(i=0;i<NUMBERS;i++)
{
for(j=i;j<NUMBERS;j++)
{
if(*(lotto_num+i) > *(lotto_num+j))
{
temp=*(lotto_num+i);
*(lotto_num+i)=*(lotto_num+j);
*(lotto_num+j)=temp;
}
}
}
printf("Your lucky numbers are as follow:\n");
for(i=0;i<NUMBERS;i++)
{
printf("%d ",lotto_num[i]);
}
}
void comparenumbers(int *lotto_num,int *winning_num)
{
int i;
int j;
int c;
int b;
int g;
c=0;
b=42;
g=0;
for(i=0;i<NUMBERS;i++)
{
for(j=0;j<NUMBERS;j++)
{
if(*(lotto_num+i) == *(winning_num+j))
{
c++;
}
}
}
for(i=0;i<NUMBERS;i++)
{
if(*(lotto_num)==b)
{
g++;
}
}
if(c==6)
{
printf("JACKPOT!!!");
}
if(c==5)
{
printf("HOLIDAY!!!");
}
if(c==4)
{
printf("NIGHT OUT!!!");
}
if(c==3&&g==1)
{
printf("CINEMA TICKET!!!");
}
if(c==4&&g==1)
{
printf("WEEKEND AWAY!!!");
}
if(c==5&&g==1)
{
printf("NEW CAR!!!");
}
if(c<3)
{
printf("MAYBE NEXT TIME QQ :(");
}
}
void frequencynumbers(int *lotto_num)
{
}
void exit()
{
exit(0);
}
Your comparison is invalid:
if ( *(lotto_no+i) >0 || *(lotto_no + i ) <= 43 )
It means "if number is greater than 0 OR number is less or equal to 43.
Also you move on to the next number even if you enter incorrect number.
for(i=0;i<NUMBERS;)
{
int number = 0;
scanf("%d", &number);
// Replace OR with AND, and fix the upper comparison operator
if(number > 0 && number < 43 )
{
lotto_no[i] = number;
i++; // Only increase when number was correct
}
else
{
printf(" Please enter a value in between 1 and 42");
}
}
I'm trying task Number 1 & 5 here.
Lets see the first task
void getnumbers(int *lotto_no)
{
int i,j,temp;
printf("Enter 6 different numbers in the range 1-42\n");
for(i=0;i<6;i++)
{
printf("Enter number %d ",i+1);
scanf("%d",&temp);
for(j=0;j<i;j++)
{
while( (temp == *(lotto_no+j)) || (temp<1 || temp>42)) //Error checking
{
printf("Wrong input. Please enter a unique number in the range 1-42");
scanf("%d",&temp);
j=0;
}
}
*(lotto_no+i)=temp;
}
return
}
Now lets go for Task no 5. I'm taking another array int frequency[43]={0}. I'm creating another function void UpdateFrequency(int *, int *). In this frequnency will be updated and this will be called each time user enters a correct input combination i,e we should call this function in task 1.
void UpdateFrequency(int *lotto_no, int *frequency)
{
int i;
for(i=0;i<6;i++)
{
(*(frequency+(*(lotto_no+i))))+=1; //frequency[lotto_no[i]]+=1;
}
return;
}
void frequencynumbers(int *lotto_no, int *frequency)
{
int i;
for(i=0;i<6;i++)
{
printf("The frequency of number %d is %d\n",*(lotto_no+i),*(frequency+(*(lotto_no+i))));
}
return;
}
Here is one way you could go about answering your first question:
void getnumbers(int *lotto_no)
{
int i;
printf("Enter your numbers\n");
for(i=0;i<NUMBERS;i++)
{
do
{
printf("Please enter number %d, between 1 and 42\n", i+1);
scanf("%d", lotto_no+i); // or &lotto_no[i] if you prefer
}
while ( *(lotto_no + i) < 1 || *(lotto_no + i ) > 42 ); // or lotto_no[i]
printf("Number %d accepted\n", i+1);
}
}
I have removed the unnecessary & and * from your scanf as they are redundant. I reversed the inequalities so that the inner do while loop will continue for each number until a valid input is given.
An easy way to count the number of times a given number is entered would be to have an array of bins, one for each number. Then you could scanf into a temporary number and increment the corresponding element of bins:
void getnumbers(int *lotto_no)
{
int i, n, bins[42] = {}; // initialise zeroed array
printf("Enter your numbers\n");
for(i=0;i<NUMBERS;i++)
{
do {
printf("Please enter number %d, between 1 and 42\n", i+1);
scanf("%d", &n); // read into temporary variable
}
while ( n < 1 || n > 42 );
lotto_no[i] = n; // assign to lotto_no
++bins[n-1]; // increment corresponding counter
printf("Number %d accepted\n", i+1);
}
for(i=0; i<42; ++i)
{
if (bins[i] > 0) printf("number %d entered %d times\n", i+1, bins[i]);
}
}