Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 3
There are 1 users currently browsing forums.
C and C++ Programming C and C++ are both robust and hugely popular programming languages that are used in multiple facets of programming, ranging from games to operating systems to simple text editors. To dicuss and seek advice, ask around here.

Reply
  #1  
Old 10-29-2009
amirpro's Avatar
Toddler
 
Join Date: Jul 2008
Location: Egypt
Posts: 8
Rep Power: 0
amirpro is on a distinguished road
I wanna help to write this program in C

Program:
In one university, it has rule that at least 75% of students in each course must pass. this means that the pass mark for each course will differ .Write a program that will read 100 students, marks and determine the pass mark.


I will be thankful if oyu could help me
Reply With Quote
  #2  
Old 11-20-2009
Toddler
 
Join Date: Nov 2009
Posts: 16
Rep Power: 0
gregr is on a distinguished road
Re: I wanna help to write this program in C

I can help you write this program but only in c++ if that is okay?
Reply With Quote
  #3  
Old 11-20-2009
vento's Avatar
Sexy monkey
 
Join Date: May 2009
Location: Lithuania
Age: 16
Posts: 185
Rep Power: 1
vento is on a distinguished road
Re: I wanna help to write this program in C

I think in this case between C and C++ would be reading file (or wherever you read the data input from).

First thing to ask is what mark system is it? I guess it's 1-10 marks system.
I gonna write the program as there would be pure data in input file (100 numbers separated by '\n' or ' '.

Here are definitions for the code I gonna write. Definitions are great so you can easily change some options in your code:
Code:
#define MAX_MARK 10
#define STUDENTS 100
Another thing is that this matters as there is no vector in C, so we must use array. Let's say:
Code:
int m[MAX_MARK + 1]
We use int because we gonna count how many students got mark i (where is stands in m[i]). We also write 11 instead of 10 because 4 bytes waste will save us some readability in code.

so now we must fill this array. First we open the file (I'll skip file opening, I would use freopen(). It's debugging-friendly) and then read the data:
Code:
int t; //temporary variable
for( int n = 1; n <= STUDENTS; n++ ){
    scanf( "%i", t );
    m[t]++;
}
There is another option to read all students to another array and then use the loop to go through, but isn't it a waste of time and memory?

So know we have counted how many students go mark i. Next we gonna find the lowest mark to pass (as I understand that's what you need):
Code:
int passed = 0;
int i; //declaring this variable so
       //it's value will be saved after loop
for( i = MAX_MARK; i >= 1; i++ ){
    passed += m[i];
    if( passed >= 75 )
        break;
}
After this loop we should have i as the mark that is the lowest to pass. Now I though that we could define that 75, but whatever, this code is very short and actually there might be no declarations, that's up to you.
About that break. Some people say that continue and break should be avoided, but I find them very useful. If we wanted to avoid them, we could use while() or do...while() loop:
Code:
int passed = 0;
int i = MAX_MARK;
while( passed < 75 ){
    passsed += m[i];
    i--;
}
i++;
Code:
int passed = 0'
int i = MAX_MARK;
do{
    passed += m[i];
    i--;
}while( passed < 75 );
i++;
As you can see in those cases I use only passed < 75 and there is no i >= 1 because if it reaches 1, it means it will have counted all 100% students, so it will stop anyway.
If you wanted to avoid those i++ after both loops, you could move i-- before passed += m[i] and set i to MAX_MARK+1 at the beginning, like this:
Code:
int passed = 0;
int i = MAX_MARK + 1;
while( passed < 75 ){
    i--;
    passsed += m[i];
}
Code:
int passed = 0'
int i = MAX_MARK + 1;
do{
    i--;
    passed += m[i];
}while( passed < 75 );
Hope it helped.

P.S. if you wrote all the code into 1 function, then int t (where we read from file) could be changed to i, but that's not a good idea. I think that would be a bad programming style. I would even move that part to another function and use a reference or make m array a global one to fill it.
__________________

How to set up portable C++ IDE (Dev-C++)

Writing in C or C++ is like running a chain saw with all the safety guards removed," — Bob Gray.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ or C better program ChrisBrown C and C++ Programming 0 08-28-2007 09:47 AM
Instant Hacking (An Introduction to Python) Part 1 jacotyco Articles, Tutorials, and Guides 3 01-30-2007 11:35 AM
Program i'm trying to write Jax Web Hosting Help 3 08-21-2006 12:02 PM
Timer exercise Jeriko Yepez C and C++ Programming 9 03-06-2006 06:29 PM
what does this mean --> cc++/Socket.h??? AsianCutie C and C++ Programming 15 06-27-2005 12:47 AM