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");
I have to prevent from putting letters into scanf in my program. I tried "isalpha" but is doesn't seems to work. Here is my code:
int main()
{
float T[100] = {0};
float *pt=T;
float suma = 0, zmienna;
int rozmiar;
printf("How many numbers would you like to put in: ");
scanf("%d", &rozmiar);
if (isalpha((char)rozmiar) != 0)
{
printf("Incorrect input");
return 1;
}
else if(rozmiar>100)
{
printf("Incorrect input data");
return 2;
}
else
printf("\n Enter the number: \n");
for (int i = 0; i<rozmiar; i++)
{
scanf("%f", &zmienna);
*(pt+i)=zmienna;
}
}
/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 wanted to ask a little bit about scanf in C using Xcode IDE. If I not initially set value for variable choice, anytime I open my program and enter any choice(either 1/2) it will go to else case every time. So I check the value after select any choice then I got a strange number. Could you please take a look at my code. Thank you in advance.
Here's my actual code:
/* Bubble Sort using MPI */
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.h>
#define N 1000
double startT,stopT;
double startTime;
void showElapsed(int id, char *m)
{
printf("%d: %s %f secs\n",id,m,(clock()-startTime)/CLOCKS_PER_SEC);
}
void showVector(int *v, int n, int id)
{
int i;
printf("%d: ",id);
for(i=0;i<n;i++)
printf("%d ",v[i]);
putchar('\n');
}
int * merge(int *v1, int n1, int *v2, int n2)
{
int i,j,k;
int * result;
result = (int *)malloc((n1+n2)*sizeof(int));
/*
i : pointer of v1
j : pointer of v2
k : pointer of k
*/
i=0; j=0; k=0;
while(i<n1 && j<n2)
if(v1[i]<v2[j])
{
result[k] = v1[i];
i++; k++;
}
else
{
result[k] = v2[j];
j++; k++;
}
if(i==n1)
while(j<n2)
{
result[k] = v2[j];
j++; k++;
}
else
while(i<n1)
{
result[k] = v1[i];
i++; k++;
}
return result;
}
void swap(int *v, int i, int j)
{
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
void sort(int *v, int n)
{
int i,j;
for(i=n-2;i>=0;i--)
for(j=0;j<=i;j++)
if(v[j]>v[j+1])
swap(v,j,j+1);
}
int main(int argc, char **argv)
{
int * data;
int * chunk;
int * other;
int m,n=N;
int id,p;
int s;
int i;
int step;
int choice = 0;
//start asking user to select option between sequential or parallel version of BubbleSort
printf(":: Welcome to BubbleSort Project for CSS333 ::\n");
printf("Please select option that you prefer\n");
printf("Type \"1\" for sequential mode or \"2\" for parallel mode\n");
printf("");
fflush(stdout);
scanf("Enter here: %d", &choice);
printf("Test value of choice(should be either 1 or 2): %d\n", choice);
//end asking
if(choice == 1){
// do seq
printf("You have selected option 1 which is running BubbleSort in Sequential mode\n");
printf("Please wait...");
}
else if(choice == 2){
// do parallel
printf("You have selected option 2 which is running BubbleSort in parallel mode\n");
printf("Please wait...");
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&id);
MPI_Comm_size(MPI_COMM_WORLD,&p);
if(id==0)
{
int r;
srandom(clock());
s = n/p;
r = n%p;
data = (int *)malloc((n+p-r)*sizeof(int));
for(i=0;i<n;i++)
data[i] = random();
if(r!=0)
{
for(i=n;i<n+p-r;i++)
data[i]=0;
s=s+1;
}
startT = clock();
MPI_Bcast(&s,1,MPI_INT,0,MPI_COMM_WORLD);
chunk = (int *)malloc(s*sizeof(int));
MPI_Scatter(data,s,MPI_INT,chunk,s,MPI_INT,0,MPI_COMM_WORLD);
sort(chunk,s);
}
else
{
MPI_Bcast(&s,1,MPI_INT,0,MPI_COMM_WORLD);
chunk = (int *)malloc(s*sizeof(int));
MPI_Scatter(&data,s,MPI_INT,chunk,s,MPI_INT,0,MPI_COMM_WORLD);
sort(chunk,s);
}
step = 1;
while(step<p)
{
if(id%(2*step)==0)
{
if(id+step<p)
{
MPI_Recv(&m,1,MPI_INT,id+step,0,MPI_COMM_WORLD,&status);
other = (int *)malloc(m*sizeof(int));
MPI_Recv(other,m,MPI_INT,id+step,0,MPI_COMM_WORLD,&status);
chunk = merge(chunk,s,other,m);
s = s+m;
}
}
else
{
int near = id-step;
MPI_Send(&s,1,MPI_INT,near,0,MPI_COMM_WORLD);
MPI_Send(chunk,s,MPI_INT,near,0,MPI_COMM_WORLD);
break;
}
step = step*2;
}
if(id==0)
{
FILE * fout;
stopT = clock();
printf("%d; %d processors; %f secs\n",N,p,(stopT-startT)/CLOCKS_PER_SEC);
fout = fopen("result","w");
for(i=0;i<s;i++)
if (chunk[i] != 0)
fprintf(fout,"%d\n",chunk[i]);
fclose(fout);
}
MPI_Finalize();
}
else{
printf("Invalid value\n");
printf("Program exiting...\n");
exit(0);
}
}
This is your problem:
scanf("Enter here: %d", &choice);
You might be expecting this to display "Enter here: " then accept a number as input and store it in the variable choice. But that's not what it does.
What this does is that it goes through the formatting string ("Enter here: %d"), one character by one. For each character that is not '%', it reads a character from stdin and compares them together. If they don't match, it pushes the character back to the buffer of stdin and stops scanning.
So unless the user types in something starting with Enter here: followed immediately by a number, it fails at reading that number.
What you probably wanted to do is to:
printf("Enter here: ");
scanf("%d", &choice);
(and then read the documentation for scanf().
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");