C urgent help

Questions about programming languages and debugging
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

C urgent help

Post by l0ngb1t »

it's been 2 years and half since i coded in C, so am refreshing my skills with this exercise
a) Write a function called eq3( ) that takes 3 arguments of type
integer and return integer. This function checks if the three integers
are equal. It returns 1 if they are equal and zero otherwise


b)Use the function eq3( ) to write a program that prompts the user to
enter three positive numbers continuously until the user enters any
character and each time the program checks if they are equal to
display a suitable message on the screen
my solution was so complicated IMO can i find something simpler...
this is what i did... am not daring to test it :P it's 3:15am over here

Code: Select all

#include <stdio.h>
#include <conio.h>
int eq3(int a, int b, int c){
if(a==b)
if(b==c)
return 1;
return 0;
}

int main() {
  int notchar=1;
  char buffer[200];
  int number;
  char *not_integer;
  int ints[3];
  int i;
  do{
  for(i=0;i<3;i++){
  if(!notchar)
	break;
  while(true){
  printf("Enter an integer: ");
  scanf("%s", buffer);
  number = strtol(buffer, &not_integer, 10 );
  if (*not_integer != '\0')
  if(buffer[0]!=null && buffer[1]==null)
  {notchar=0;break;}
  else
  {
    fprintf("%s is not valid.\n", not_integer);
    continue;
  }
  if(number<0)
	{printf("you entered a negative integers, you need it to be positiv\n");continue;}
  else{ints[i]=number;break;}
  }
  }
  if(eq3(ints[0],ints[1],ints[2]))
	printf("your integers are All equal");
  else
	printf("your integers are Unequal");
	}
	i=0;
while(notchar)
}
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: C urgent help

Post by maboroshi »

As I understand it this should do what you need for the first project anyways :-)

Code: Select all


#include <stdio.h>

int eq3(int a, int b,  int c);



int main() {
	eq3(57, 63, 49); // Not equal
	eq3(56, 56, 56); // All equal
	eq3(37, 37, 49); // a and b equal the same c doesn't
	eq3(29, 43, 43); // b and c equal A does not
	return 0;
}


int eq3(int a, int b, int c) {
	if (a == b && b == c) {
			printf("The values of a, b and c equal the same\n");
	}	
	else if (a != b && b != c) {
			printf("The values do not match\n");
	}
	else {
		printf("Numbers do not equal\n");
	}
	return a, b, c;
}


Take care Mabo :-)

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: C urgent help

Post by Gogeta70 »

It would be better if eq3 returned bool instead of integer.

Here's how i'd do it:
(When i tried to post the code here AND on code.suck-o.com, i got method not implemented >_<)
" onclick="window.open(this.href);return false;
¯\_(ツ)_/¯ It works on my machine...

Post Reply