473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

shortening a program?

Is there anyway to shorten a program by calling another function i could write, instead of repeating the same section over and over??

thanks
Oct 25 '06 #1
5 3302
Banfa
9,065 Expert Mod 8TB
Yes find a section of code that is repeated, move that code to a function and then replace all the places that section of code is used with a call to the function.

This will probably produce short but slightly slower code.

You can be clever and find sections that do the same thing on different data and create a function that accepts parameters in order to pass that different data into the function.
Oct 25 '06 #2
Yes find a section of code that is repeated, move that code to a function and then replace all the places that section of code is used with a call to the function.

This will probably produce short but slightly slower code.

You can be clever and find sections that do the same thing on different data and create a function that accepts parameters in order to pass that different data into the function.

Thank you for a quick reply.
Could you tell me how i call a function? And if i put the repeated part of the code in another progam, do i write it the same way?
thanks
Oct 25 '06 #3
chiefs
8
Is there anyway to shorten a program by calling another function i could write, instead of repeating the same section over and over??

thanks
A very easy way to do this is the goto statement:

For Example:

goto NAME_1;

Follow the program & you will understand as long as this is in C++ and not Visual C++

// Program Q1:Q1.cpp
# include <iostream.h>
# include <stdio.h>

main()
{

char name[30];
char a;
char pw1;
char pw2;

printf ("What is your name?");
scanf ("%s", &name);
printf ("High (%s)\n\n", name);
printf ("Is this correct?");
scanf ("%s", &a);
if ((a == 'y') || (a == 'Y'))
goto PASS_WORD1;
else
goto NAME_1;

NAME_1:
printf ("Please Re-enter name!");
scanf("%s",&name);
printf ("Okay, %s\n\n", &name);
goto PASS_WORD1;
Oct 25 '06 #4
manontheedge
175 100+
you can use GOTO, but in my experiences, GOTO is extremely frowned upon in C and C++.

i think you're looking for FUNCTIONS. It all depends on how you write your code on how you use functions. If you have classes, then you can DECLARE all of your functions in the header file, and DEFINE them in a .cpp file.

if you just wanna bunch everything in one file, then you can declare functions before or after main(), and define them in main(), and call them there as well.

if this is at all confusing sounding, it's really not once you see some examples and try it yourself.

i don't have time to type up an example, but if you need some answers quickly, look around on the internet for functions.
Oct 25 '06 #5
Banfa
9,065 Expert Mod 8TB
Here is an example of a small program that defines and calls 1 function and also calls 1 library function.

I strongly recomend you don't use GOTO until you truely understand the C/C++ language in full and then you probably shouldn't use GOTO.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void Swap(unsigned &a, unsigned &b);
  4.  
  5. int main()
  6. {
  7.     unsigned a = 5;
  8.     unsigned b = 10;
  9.  
  10.     printf("a = %u, b = %u\n", a, b);
  11.  
  12.     Swap(a, b);
  13.  
  14.     printf("a = %u, b = %u\n", a, b);
  15.     return 0;
  16. }
  17.  
  18. void Swap(unsigned &a, unsigned &b)
  19. {
  20.     a ^= b;
  21.     b ^= a;
  22.     a ^= b;
  23. }
  24.  
Oct 26 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: kaptain kernel | last post by:
how does one shorten an array based on a key value. Say I have this: array("Car1"=>"Porsche","Car2"=>"Merc","Car3"=>"Ford","Car4"=>"Toyota"); and I want to pop "Car2" out of the array,...
4
by: Dariusz | last post by:
I have a guestbook on one of my sites where the entires are formatted to be x pixles in width. Now the text is input and output from a "blob" field from a database... but when people input long...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.