Jump to content

Not sure how to call my functions.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
palomainak

palomainak

    Newbie

  • Members
  • Pip
  • 3 posts
I'm a noob I know, but my functions aren't executing because the variables don't match up when I call them. Is there a way to call them without a return statement, or what return to use?
#include<stdio.h>

int is_prime (int no){

      int i=2;

      while(i <= no-1){

            if(no % i == 0){

                  return 0;

            }

            i++;

      }

      // i know that my number is not composite

      return 1;

}

 

void largest_prime_factor (int no){
int i;
	for(;i<=no/2;i--){

		if(no%i==0 && is_prime(i)==1){

		printf("Largest prime number that divides %d evenly is %d\n", no, i);

		}

	}
		
}

int prime_string (int no){
		int i;
      for(i=2;i<=no-1;i++){

            if(is_prime(i)==1 && no%i==0){

				//printf("Prime factors = %d * ", i);
				return 1;
            }
	
			else if(is_prime(no)==1){
				return 2;
				//printf("%d", no);

			}
		no=no/i;
	}
}
 

void main()

{

int no,i,mod, num_prime;

char input,answer;

printf("Please enter a positive integer:");

scanf("%d",&no);

scanf("%c", &input);

	while (input != 'q') {

      num_prime = is_prime(no);
	  
	 

      if (num_prime == 0) {
			int i;
            printf ("COMPOSITE\n");
			printf ("Largest number that divides %d evenly is %d\n", no/i, no);
			switch(prime_string(no)){
				case 1:
					printf("Prime factors = %d * ", i);
				case 2:
					printf("%d", no);
					break;
			}
			printf("Please enter a positive integer (or q to quit): ");
				scanf("%d",&no);
				scanf("%c", &input);
		


      }

      else {

            printf ("PRIME\n");
			printf("Please enter a positive integer (or q to quit): ");
				scanf("%d",&no);
				scanf("%c", &input);

      }
	  
}

Edited by WingedPanther, 22 February 2009 - 03:41 AM.
add code tags (the # button)


#2
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
Use the code tag (# button), use indentation or it's really hard to read. I think you need to research upon scope (which variables can be accessed from which part of the code., global variables, etc).

I also cannot tell if that's C or C++ you use void main, returns, etc but that's not needed in C, but then you use printf, scanf, etc and they are C statements.

As to your case, always make a default case, even if it won't be used.
Posted Image

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I've reformatted your code and added some comments. Note: your code is NOT valid C, even though you are using C-style input/output.
#include<stdio.h>

int is_prime (int no){
  int i=2;
  while(i <= no-1){  // this could be i<=no/2
    if(no % i == 0){
      return 0;
    }
    i++;
  }
  // i know that my number is not composite
  return 1;
}

 

void largest_prime_factor (int no){ //this function may return 0 at all times!
int i;
	for(;i<=no/2;i--){ //i is never initialized!  potentially unpredictable behavior!
		if(no%i==0 && is_prime(i)==1){
		printf("Largest prime number that divides %d evenly is %d\n", no, i);
		}
	}
}

int prime_string (int no){
	int i;
  for(i=2;i<=no-1;i++){
    if(is_prime(i)==1 && no%i==0){
				//printf("Prime factors = %d * ", i);
				return 1;
    }
			else if(is_prime(no)==1){
				return 2;
				//printf("%d", no);
			}
		no=no/i;
	}
}
 

void main()  //should really be int main()
{
  int no,i,mod, num_prime;
  char input,answer;
  printf("Please enter a positive integer:");
  scanf("%d",&no);
  scanf("%c", &input);
	while (input != 'q') {
    num_prime = is_prime(no);
    if (num_prime == 0) {
			int i;
      printf ("COMPOSITE\n");
			printf ("Largest number that divides %d evenly is %d\n", no/i, no); //i has not been initialized!
			switch(prime_string(no)){
				case 1:
					printf("Prime factors = %d * ", i); //i has not been initialized!  Prints at most one prime, not all!
				case 2:
					printf("%d", no);
					break;
			}
			printf("Please enter a positive integer (or q to quit): "); //misleading instructions... 2 inputs required!
				scanf("%d",&no);
				scanf("%c", &input);
    }
    else {
      printf ("PRIME\n");
			printf("Please enter a positive integer (or q to quit): ");
      scanf("%d",&no);
      scanf("%c", &input);
    }
  }
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog