473,400 Members | 2,145 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,400 software developers and data experts.

Last teLoop issue using arrays

I am trying to write an array in c that stores the last ten command entered in a program. To do this i created an eleven element global array, as different functons need to act on it. i am tring to shif the elements in the array to the right by one posistion. what i end up with is the last two commands. the first one is in the right place but the next is repeated 9 times. here is the code.. can you help me figure out what im doing wrong..?
Expand|Select|Wrap|Line Numbers
  1. int CHOICE;
  2. int LTENC[11];
  3.  
  4. void cArrayModify()
  5. {
  6.  
  7.  
  8.     for(int i=0;i<10;i++)
  9.     {
  10.         int tempValue=LTENC[i];
  11.         int copyLocation = i + 1;
  12.         LTENC[copyLocation]=tempValue;
  13.         i++;
  14.     }
  15.  
  16.  
  17.     LTENC[0]=CHOICE;
  18. }
  19.  
  20. void displayLastTenChoices()
  21. {
  22.     printf("\n \n Last ten choices: ");
  23.     for(int i=0;i<10;i++)
  24.     {
  25.         printf(" %d", LTENC[i]);
  26.     }    
  27.     printf(" \n");
  28. }
Mar 3 '08 #1
9 1579
weaknessforcats
9,208 Expert Mod 8TB
for(int i=0;i<10;i++)
{
int tempValue=LTENC[i];
int copyLocation = i + 1;
LTENC[copyLocation]=tempValue;
i++;
}
So, the loop starts with i=0. That means:
int tempValue=LTENC[0]; //original element 0
int copyLocation = 0 + 1; //target location
LTENC[1]=tempValue; //element 0 moves to element 1

OK but you didn't save element 1. It just got overwritten.

What you need is a circular array. That is, you need a counter that says where the array "starts". You add to the array by adding to the start-1 position.
The counter has to range from 0 to 10, and when 10 goes back to zero.

The last 10 commands are displayed by reading the array backwards from the start-1 location to the start location.
Mar 3 '08 #2
Parul Bagadia
188 100+
M not so sure about it; but i think in the first for loop you have done right shifting; and incremented i by one; so i think you should check for the limit of i, what i mean is; you start from first element instead of 0th. So your condition in for loop should be i<11.....so that ull start with 1st element and end up with 10th......
Mar 3 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
True, but after 10, you have to wrap around to the start of the array. The 11th choice is in element 0. The 10th choice is in element 9.

If you don't write a cricular buffer, then you can't show the "last 10" choices.
Mar 3 '08 #4
So i should add code to store element 1 like so?
for(int i=0;i<10;i++)
{
int j = i+1;
int tempValue0=LTENC[i];
int tempValue1=LTENC[j];
int copyLocation = i + 1;
LTENC[copyLocation+1]=tempValue0;
LTENC[copyLocation]=tempValue1;
i++;
}
Mar 5 '08 #5
Im not sure what you mean by "circular buffer" all i want to do is have a place to store the choices. Each execution of the function will run ten times therefore adding one choice to the array and knocikng the " tenth" choice off.
Mar 5 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
"circular buffer"
A circular buffer is an endless buffer.

For example, you need to keep the last 3 commands. Let's call them A, B, C etc as they come in. Let's also assume you use a 3-element char array.

The start is element 0. You out the command A in element 0 and make the start element 1.
0 A
1 AB
2 ABC
Start is now 0 again.
0 DBC
1 DEC
2 DEF
Start is now 0 again
0 GEF
1 GHF
2 GHI

So, the number is the start of the buffer. To read the buffer, you begin at the start+1, read to end of the array by wrapping around to the beginning and reading to the start.

So GHF with a start at 1 reads FGH. DBC with a start at 0 reads BCD.


Once you figure this out, you put it in a function and it just works. You could even write a C++ circular buffer class based on a linked list.
Mar 5 '08 #7
MACKTEK
40
for(int i=0;i<10;i++)
{
int tempValue=LTENC[i];
int copyLocation = i + 1;
LTENC[copyLocation]=tempValue;
i++;
}
Logically I ask myself where is the first command? is it LTEN[0] or [9]:
lets say LTEN[0] = 100
now using your code and going thru the first iteration:
tempValue = 100
copyLocation = i+1 , which is 1
then you do LTENC[1]=tempValue;
therefore you assigned LTENC[1] the value that the old LTEN[0] had.

then you increment...
the problem is, that the next loop simply reassigns...

LTENC[1] = 100
now using your code and going thru the next iteration:
tempValue = 100
copyLocation = i+1 , which is 2
then you do LTENC[2]=tempValue;
therefore you assigned LTENC[2] the value that the old LTENC[1] had.

this problem will repeat itself.

What you would want to do is something like:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<9;i++) // NOTICE I lowered the max...
  2.     {
  3.         LTENC[i]=LTENC[i+1];
  4.          i++;
  5.     }
  6. LTENC[9]= newval;
  7.  
lets assume from 0 to 9 contains 10,11,12,13...etc.
iterating for the first few we get...
LTENC[0] = 11
LTENC[1] = 12
...
LTENC[9] = newval

the next time they enter a new value... each element in the array would move down 1.


I hope that helps, if I made a mistake please let me know.
Mar 6 '08 #8
Thank you MackTek.... You answered my question completely.. I was trying to do a right shift when all i need to do was change the loop and shift left. Thanks again for all who tried to help.
Mar 6 '08 #9
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<9;i++)
  2.     {
  3.         LTENC[i]=LTENC[i+1];
  4.  
  5.     }
  6. LTENC[9]=CHOICE;
  7.  
  8. }
I figured it out.... With the example above before the new val is assigned there is an extra increment resultion in the following ouput.

0,0,0,0,0,0,0,0,0,1

the first nine will remain zero.. with the increment removed we get the desired affect
1,2,3,4,5,6,7,8,9,0

Thanks for all your help guys. And thanks again MackTek for the information.. IO would have been chasing my tail for weeks trying to figure this one out.
Mar 6 '08 #10

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

Similar topics

6
by: Bimo Remus | last post by:
Hi, I am currently taking a C++ class and am having problems with a homework assignment. My problem is that I need to pull the first and last words out of of a character string array which is in...
17
by: michel.ank | last post by:
Hi, I'm using the class PrintLines and my last record of page aren't with the borders. Somebody can help me? Thanks,
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
7
by: daniel | last post by:
Hello , I always had the feeling that is better to have char arrays with the size equal to a power of two. For example: char a_str; // feels ok char b_str; //feels not ok.
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.