home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C programming question

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1603
Discussions: 200,988, Posts: 2,379,867, Members: 246,357
Old April 5th, 2009, 03:53 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Apr 2009
Posts: 11
C programming question

Ello ello, I'm taking a first term programming class using C and am having a little trouble with one of our final assignments. We are supposed to creat a program that goes through an array of our creation and reverses the order of the numbers in said array.

The example we were given was

original array (element 0 first): 10 20 30 40 50 60

after exchange (element 0 first): 60 50 40 30 20 10

however we are suppost to make it so that it will also work if
an odd number is imputed as well.

In it we should use the size_t (sizeOf()) and only one array.
Any help would be greatly appreciated because I don't even have the slightest idea how to start this <,<
valk181 is offline   Reply With Quote
Old April 9th, 2009, 10:33 AM     #2 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,242
Send a message via AIM to Rootstonian
What do you mean by "odd" number? Like the number 11 or an odd number of elements in the array? Are you supposed to get an array size from the user via a prompt?

At this level, your professor is wanting you to loop through arrays. I'm sure you've covered the usage of the "for" loop in C. Use it (hint: if it counts up, it can count down too)
Rootstonian is offline   Reply With Quote
Old April 9th, 2009, 08:17 PM     #3 (permalink)
Junior Member
 
Join Date: Apr 2009
Posts: 11
By odd I ment had their been 7 arrays or whatever.

I ended up with my code like this
/*
Name: Prog015
Copyright:
Author: Jake
Date: 04/09/09 17:43
Description: arrays!
*/
#include <iostream>
using namespace std;
#define sizeOfArray(y) (sizeof y / sizeof y[0])
int exchangeArrays(int y, int z);
int array[] = { 10, 20, 30, 40, 50, 60, 70};
int main()
{
int arrayLength = sizeOfArray(array);

cout << "Original array (element 0 first): ";

for (int x=0;x<=arrayLength+arrayLength/2;x++)
{
if (x < arrayLength)
{
cout << array[x] << " ";
}
if (x == arrayLength)
{
cout << "\n" << endl;
}
if (x > arrayLength)
{
exchangeArrays(x-(arrayLength+1),
(arrayLength-(x-(arrayLength+1))-1));
}
}

cout << "Swapped (element 0 first): ";

for (int x=0;x<=arrayLength-1;x++)
{
cout << array[x] << " ";
}

cout << "\n" << endl;

system("PAUSE");
}


int exchangeArrays(int y, int z)
{
int temp=array[y];

array[y]=array[z];
array[z]=temp;
}

but thats just so epically long, any easier way to go about it?
valk181 is offline   Reply With Quote
Old April 10th, 2009, 08:09 AM     #4 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,242
Send a message via AIM to Rootstonian
Yeah, that's a bunch of code. Given length = the size of the array:

// Print it forwards
for(int i=0; i<=length; ++i)
cout << array[i];

// Print it backwards
for(int i=length; i>=0; --i)
cout << array[i];

You know what, I might have misread your post. Was it just PRINTING them in different order or was it to change the ACTUAL array? My bad if it was the latter.

You might have been able to do the swap in the for loop too; it may have needed 2 for loops based on odd or even number of elements in the array (not sure unless I REALLY code it), but:

// Not sure if k=lenght is valid; been a while since I did C
for(int i=0; i<=length,k=length; ++i {
int temp = array[i];
array[i] = array[k];
array[k] = temp;
--k;
}

If you get bored on break, do it your way with the call to the exchange procedure and my way with 1 or 2 for loops. Do it for an array of a million items. Time it
That's when Computer Science gets fun; you may find the overhead to call your procedure really slows the program down on a huge array.

Last edited by Rootstonian : April 10th, 2009 at 08:39 AM.
Rootstonian is offline   Reply With Quote
Old April 15th, 2009, 10:09 AM     #5 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
Post

you actually approached the solution perfectly. The only reason it's so large is because of your coding practices & style. first of, when posting code you should put it in code tags. These look like "[ code ]" and "[ /code ]" without the " and spaces. Makes it much easier to read!

also your for loops while printing are a little bit crazy and unnecessary. Overall very good work for a first year programmer

here it is cleaned up a little

Code:
/*
Name: Prog015
Copyright:
Author: Jake
Date: 04/09/09 17:43
Description: arrays!
*/

#include <iostream>
using namespace std;

#define sizeOfArray(y) (sizeof y / sizeof y[0])

int exchangeArrays(int y, int z);
void print_array()

int array[] = { 10, 20, 30, 40, 50, 60, 70};

int main()
{
	int arrayLength = sizeOfArray(array);

	cout << "Original array (element 0 first): ";
	print_array();

	for (int x=0; x<arrayLength/2; x++)
		exchangeArrays(x, arrayLength-x);

	cout << "Swapped (element 0 first): ";
	print_array();

	system("PAUSE");
}

void print_array()
{
	int arrayLength = sizeOfArray(array);
	for(int x=0; x<arrayLength; x++)
		cout << array[x] << " ";
	cout << "\n";
}


int exchangeArrays(int y, int z)
{
	int temp=array[y];
	array[y]=array[z];
	array[z]=temp;
}
Again that's the exact same thing you had, just cleaned up a little Nice work.
sr71000 is offline   Reply With Quote
Old April 15th, 2009, 06:11 PM     #6 (permalink)
Junior Member
 
Join Date: Apr 2009
Posts: 11
Haha, well thank you.

I really hate for statements, I wish whiles were easier to move around right <,<

Ty for the help though
valk181 is offline   Reply With Quote
Old April 17th, 2009, 11:43 AM     #7 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
You'll learn that each looping style has a purpose. You'll come to love each any every one of them as you learn to use them properly .

Are you sticking with programming or is this just one of those one time shot courses?

-Kevin
sr71000 is offline   Reply With Quote
Old April 17th, 2009, 11:52 PM     #8 (permalink)
Junior Member
 
Join Date: Apr 2009
Posts: 11
I don't really know to be honest, I'm switching colleges for next semester and my comp science major has something different listed than what I've done so far.

So far I've had... one semester of Java, C++, and VB. I'm not really to sure what I'll have to go through next haha
valk181 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming question RustedPC Certification and Education 5 April 21st, 2009 08:51 PM
General programming question lost-and-found Webmastering and Programming 3 May 19th, 2007 09:21 AM
just a quickie programming question: oliver w Webmastering and Programming 2 December 23rd, 2005 04:24 AM
Question about programming with TV card? carfield Multimedia and Audio 1 May 23rd, 2003 09:07 AM
Programming RPGs with dx 8.0 question I want PRPGWDX Webmastering and Programming 0 October 26th, 2002 01:00 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3066)
Charges against non-tippers dropped.. (20)
Health Care Rationing (11)
Delete an OS (17)
Nvidia GTX 260 problem (10)
Laptop with wireless problem. (12)
windows vista security holes (19)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (8)
windows 7 problem (7)
[F@H SPAM 11/16/09] ! 1/2 months to.. (39)
Internet Lost (5)
Recent Discussions
Jedi Academy Problem (3)
Desktop Calendar Application (1)
Can a page file be "too big".. (1)
Nvidia GTX 260 problem (10)
Point and Shoot Camera Suggestions. (8)
Looking for new motherboard (0)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (39)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)


All times are GMT -4. The time now is 06:53 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28