473,836 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code Critique Please

I have an assignment due mid next week that I have completed. I was hoping
someone could take a look at the code and tell me what they think of the
style. Id like to know if this is good code that I can be proud of or if my
technique still needs some work. What can be improved? I created an htm
page that first lists the assignment, and under that is my code. I think
the code is good, but Ive thought that before...


Jul 19 '05 #1
3 2308
One thing I forgot to mention is that I need not be concerned with input
errors. The teacher said not to bother programming any catches and such to
trap input errors, so please keep that in mind when viewing.

Jul 19 '05 #2
Rv5 wrote:
I have an assignment due mid next week that I have completed. I was hoping someone could take a look at the code and tell me what they think of the
style. Id like to know if this is good code that I can be proud of or if my technique still needs some work. What can be improved? I created an htm
page that first lists the assignment, and under that is my code. I think
the code is good, but Ive thought that before...

In future, please post your source here, and skip the assignment question.
Either report a bug, syntax error, a design you can't achieve, or a request
about style.

Warning: So many kids ask this newsgroup "please do my homework for me" that
questions about homework should be phrased carefully.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
That line defeats the purpose of namespaces. Write

using std::string;

etc. for each individual identifier you want to import. You'l find
surprisingly few of them.
int f; // max number of frames
That kind of comment is a "code smell" that the indentifier it comments
could have a better name. Try:

int max_number_of_f rames;
int l; // length of reference string
string r; // reference string
Give variables the narrowest scope possible. These shouldn't be outside the
functions that use them.
int numfaults = 0; // number of page faults
char *frame; // frames that will contain the letters
int *counter; // counts letter time in the frame since last referenced
Students should start with STL and container classes like std::vector. They
should not need pointers for the first several assignments. Read
/Accelerated C++/ to learn these features in the right order.
bool analyzeFrames(i nt i)
for (int k = 0; k < f; k++)
// first trys to find the page in the frame
if (frame[k] == r[i])
This function is too long. A good place to break it is one of the inner
controlled block. Everything this 'if' controls could be inside a subsidiary
counter[k] = 1; // finds the letter, resets its counter
for (int l = k+1; l < f; l++) // loaded pages time in frame continues counter[l]++; // increment other page counters
return true;
// if the page is not loaded, see if there is a blank frame to load it in else if (frame[k] == '-')
frame[k] = r[i]; // fill the empty cell with next page
counter[k] = 1; // set that page counter to 1
numfaults++; // page was not in the frame, so a page fault occurs
return true;
counter[k]++; // increments counter, page not used this time
numfaults++; // page not found and could not be loaded, page fault occurs return false;

void replace(int i)
// largest counter index is the frame that gets replaced
int largest = counter[0];
int index = 0;

// loop through counter array
for (int k = 1; k < f; k++)
if (counter[k] > largest) //finds the largest counter
largest = counter[k];
index = k;

frame[index] = r[i]; // replaces least recent page with new one
counter[index] = 1; // sets new page counter to 1

int main()
// user enters values of variables
This function is also way too long. The first block of it, the input stuff,
could be inside a function called user_enters_val ues_of_variable s().
cout << "Input maximum number of frames (f): ";
cin >> f;

cout << "Input length of reference string (0 < l < 101): ";
cin >> l;

cout << "Input reference string (r): ";
cin >> r;
Duplicated code (like this cout-cin sequence) is a sign there could be a
function. In this case, the function could call like this:

f = getValue("Input maximum number of frames (f): ");
// displays the values the user entered
cout << "f = " << f << endl;
cout << "l = " << l << endl;
cout << "r = " << r << endl;

// creates the proper number of cells in the frame and counter arrays
frame = new char[f];
counter = new int[f];
Nobody deletes these. Use std::vector (even if your professor did not cover
them yet). If in the rare chance you actually need an array, use 'delete[]
frame' to release the storage when you are done with it.
// fills the frame and counter arrays with '-' and 0 respectively
for (int i = 0; i < f; i++)
frame[i] = '-';
counter[i] = 0;
cout << frame[i]; // verifies that the frame is empty
cout << endl;

// main loop that is going through the reference string
for (int i = 0; i < l; i++)
if(!analyzeFram es(i)) // attempts to find page or blank cell to load it to {
replace(i); // not found, no empty cells, replace least recent page
// displays contents of frame as the program runs
for (int l = 0; l < f; l++)
cout << frame[l];
cout << endl;

cout << "Total page faults = " << numfaults << endl;

return 0;

Like I used to say, as a lab aide, when finishing attending to a student:
"Good luck!"

Jul 19 '05 #3
On Sat, 15 Nov 2003 16:20:33 -0800, Rv5 wrote:


This URL is not accessible. I get the error message:

"Access to the port number given has been disabled for security reasons"
Remove your rose colored glasses before e-mailing me

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Arthur Pratz | last post by:
Hi all, I have been working on this site for a while now. I am posting on this newsgroup for honest reviews please. I will be working on the store link with accepting credit cards online. So any tips for making an online checkout would be helpful. I will be using Miva Merchant to do that. Thanks for your time, Mike Pratz http://www.chowardcompany.com
by: Cynthia Turcotte | last post by:
Hi all -- A client has hired me to, among other things, optimize her web site for search engine submission. So being the dutiful SEO geek that I am, I went through and optimized each and every page of the site, including a complete recoding of the homepage which she botched. Now after all of this work, she tells me that she wants to keep the home page as it is. I've gone on record telling her that I feel that she won't get the...
by: Eric | last post by:
There is a VB.NET critique on the following page: http://www.vb7-critique.741.com/ for those who are interested. Feel free to take a look and share your thoughts. Cheers, Eric. Ps: for those on comp.programming, this may be off topic, but I've posted there because the critique was part of a discussion in that group.
by: TC | last post by:
Are there any good sites or forums for a web critique? I went to alt.html.critique and it's pretty dead.
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much memory, but it should certainly handle 64-bit cpus :) #include <stdio.h> #include <stdlib.h> char * to_binary (unsigned long value) {
by: G Patel | last post by:
I wrote the following program to remove C89 type comments from stdin and send it to stdout (as per exercise in K&R2) and it works but I was hoping more experienced programmer would critique the layout/style/etc. Any comments will be helpful. Thank you. /* **************************************************** C89/90 COMMENT REMOVER
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
by: Warhawk012 | last post by:
Hey all, I've started a portal site for the Embroidery community (both professional and hobbyist), and I'm looking for some site comments and critique. The site is at http://www.embuniverse.com/ If some of you can help me out and tell me if the site flows nicely, and whether or not the placement of the menu items is correct (fiddled
by: winston | last post by:
I wrote a Python program (103 lines, below) to download developer data from SourceForge for research about social networks. Please critique the code and let me know how to improve it. An example use of the program: promptpython download.py 1 240000 The above command downloads data for the projects with IDs between 1
by: matt | last post by:
this is my first program in this language ever (besides 'hello world'), can i get a code critique, please? it's purpose is to read through an input file character by character and tally the occurrence of each input character. it seems to compile and run, so i'm looking for the opinions of old-timers here plz. /* * File: occurrenceTally.cpp * Author: matthew *
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.