C programming question  | |
April 5th, 2009, 03:53 PM
|
#1 (permalink)
| | Junior Member
Join Date: Apr 2009
Posts: 11
|
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 <,< |
| |
April 9th, 2009, 10:33 AM
|
#2 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,242
|
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) |
| |
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? |
| |
April 10th, 2009, 08:09 AM
|
#4 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,242
|
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.
|
| |
April 15th, 2009, 10:09 AM
|
#5 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
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. |
| |
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  |
| |
April 17th, 2009, 11:43 AM
|
#7 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
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 |
| |
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  |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |