GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Sep-2004, 13:03
kitin21 kitin21 is offline
New Member
 
Join Date: Sep 2004
Location: manila, philippines
Posts: 3
kitin21 is on a distinguished road

help in c programs


hi! i'm a newbie hir. and i really need your help in this. we were given a case study and we should submit it as early as possible, can you help me in creating programs for this? here are the items that i wasn't able to make programs yet.. pls..pls.. help..


if possible, use functions

1. program that will accept numbers until a negative value is entered then determine the highest, lowest, sum and average of all given numbers. (negative value plays terminator only and it will not be computed)

2. program that will accept a number and determine if it's composite or prime. (prime means only 1 factor and number itself, and the composite has many factors)

3. program that accept a string and determine if it is a palindrome or not. (palindrome means the word is the same even if read backwards)


maybe simple for some but kinda hard for me since i have study c language for barely a month only.. tnx guys!
  #2  
Old 04-Sep-2004, 14:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Of the many (dozens, hundreds, thousands, ...) of ways to perform the tasks on your list, you should choose methods that your instructor gave to you in class or in examples, or in your book or ???

Simple methods for obtaining input from program users at execution time are invariably inappropriate for polished, finished programs.

So what have you covered in class? Was it with a function named scanf()?
Have you covered getchar()? What?

Were there any examples of obtaining input data from users?

Were there any examples of putting text from a program to the user? (Have you seen printf("Hello, world!\n"); or something similar?)

What Operating System (Windows XP or what?) What Compiler?

Dave
  #3  
Old 05-Sep-2004, 12:45
kitin21 kitin21 is offline
New Member
 
Join Date: Sep 2004
Location: manila, philippines
Posts: 3
kitin21 is on a distinguished road
With almost 1 month of lectures we've covered basic looping functions(while, do while, else, if, switch). And yes, we were also thought the scanf() function.

Regarding program #1 i am able to determine what is the highest and lowest input value, But i am encountering problems on how to get the sum and average of all the input values.(Note: there is no limit to the number of input values a user can enter, not until a negative value has been entered.)

Program #2
I am having trouble figuring out the formula using the basic mathematical operators to determine if the input value is prime or composite.

Program #3
I was told that the getchar() function will be used on this program, however we have not yet discussed the said function. But i am already asking help for this program so that i will have some advanced knowledge on how to create the program.

All help, suggestions, information will be most helpful.
  #4  
Old 05-Sep-2004, 13:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by kitin21
With almost 1 month of lectures we've covered basic looping functions(while, do while, else, if, switch). And yes, we were also thought the scanf() function.

Regarding program #1 i am able to determine what is the highest and lowest input value, But i am encountering problems on how to get the sum and average of all the input values.(Note: there is no limit to the number of input values a user can enter, not until a negative value has been entered.)

Program #2
I am having trouble figuring out the formula using the basic mathematical operators to determine if the input value is prime or composite.

Program #3
I was told that the getchar() function will be used on this program, however we have not yet discussed the said function. But i am already asking help for this program so that i will have some advanced knowledge on how to create the program.

All help, suggestions, information will be most helpful.


I would suggest that you post some code. People on this forum are usually glad to point out errors and give suggestions for improvement.

Program #1. How do you get the sum and average? Well, here's the program approach (you supply the C/C++ code).

1. Start with a value of sum = 0. You also need a variable that will keep track of the number of inputs; Call the variable NumberOfInputs, and set it equal to 0 initially.

2. Now you make a loop. You already indicate that you know how to get the max and min of all of the inputs. Great!!! Now you need only a couple of extra statements inside the loop. Suppose the current value is in a variable named, for example, InputValue. Try the following

CPP / C++ / C Code:
  sum += InputValue; /* or you could say sum = sum + InputValue */
  NumberOfInputs++; /* or you could say NumberOfInputs = NumberOfInputs + 1 */

3. After you exit the loop you can see that the variable sum contains the sum of all of the input values, and the variable NumberOfInputs contains --- guess what? --- the number of inputs. So you can then calculate the average by something like

CPP / C++ / C Code:
Average = sum / NumberOfInputs;

Note that if your inputs are integers (that is, sum is an int), you don't want to calculate the average using integer arithmetic, so you may have to do something special to make sure the arithmetic is floating point.


Problem #2: Given an integer greater than 0, you want to see if it is divisible by anything other than 1 and itself (you don't have to check to see if it is divisible by 1, and you don't have to check to see if it is divisible by itself, since these two things are always true. Now, there is a neat operator in C called the modulus operator, %.

The way it works is easy to understand. Suppose x, y, and z are ints, and y is not equal to zero. Then consider the expression

CPP / C++ / C Code:
  z = x % y

Now z is equal to the remainder that you get when you divide x by y.

So, you can see that if z is equal to zero, then x is divisible by y, since that happens if (and only if) the remainder is zero.

So, given an int, say x, a "naive" approach to determining whether x is a prime number is to see if it is divisible by anything between 2 and x - 1. Make a loop that contains a calculation that checks the remainder. If you get a remainder of zero anywhere in the loop, you know that x is not a prime number (that is, it's a composite number).

Why did I say the above approach is "naive"? Well, this is a direct application of the definition of prime number. Methods abound that are computationally more efficient. My maxim: Do it first, optimize later (if there is a payback commensurate with the extra effort.)

Note that for this program, all numbers are type int. The % operator is meaningless for floats and doubles.

Program #3:

The C language has no "string" data type. Strings are (almost) always represented as an array of chars. The last char in a string is the byte 0x00.

Write a string on your paper, say:

"This is a string"

Now, start at the beginning, swap the first and last characters. Go to the second string and swap the second and next-to-last characters. How far do you have to go before you are through?

Be sure to test this with strings having odd numbers of characters and with strings having even numbers of characters. See the difference?

Now how do you make a program to do this? Well give it a shot.

Good Luck!!!!!!!

Dave
Last edited by davekw7x : 05-Sep-2004 at 14:21.
  #5  
Old 08-Sep-2004, 08:38
kitin21 kitin21 is offline
New Member
 
Join Date: Sep 2004
Location: manila, philippines
Posts: 3
kitin21 is on a distinguished road
tnx so much davekw7x. wat you gave me really help me in my problem. now i was able to figure out wat code i am going to use. and glad that i join this forum. tnx!
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Adsense could mean death to affilliate programs jrobbio AdSense Forum 12 11-Feb-2006 16:51
Using datafeeds for affiliate programs Div MySQL / PHP Forum 23 05-Mar-2004 23:37
Programs that need expert eye C++ Forum 3 16-Jan-2004 06:02
premade forums and other premade php programs invaderz MySQL / PHP Forum 1 06-Jul-2003 03:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.