Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:15:00+00:00 2026-06-04T19:15:00+00:00

I wanted to create 2 global arrays which can be updated during the run

  • 0

I wanted to create 2 global arrays which can be updated during the run of the programme.In each update i add one element to zeroth position and deleted the last number
I created the arrays as….
In the .h file……….
//////////////

@interface Shared : NSObject{
NSMutableArray *x;
NSMutableArray *y;
}

@property (nonatomic,retain) NSMutableArray *x;
@property (nonatomic,retain) NSMutableArray *y;
+(Shared*)sharedInstance;

@end

In .m file

staticShared* sharedInstance;
@implementation Shared
@synthesize  x; 
@synthesize  y;

+(Shared*)sharedInstance
{
if (!sharedInstance) {
sharedInstance=[[Sharedalloc]init];
    }
returnsharedInstance;
}

-(Shared*)init
{
self = [superinit];
if(self)
    {
x=[[NSMutableArrayalloc] init];
x=[NSMutableArrayarrayWithObjects:@"0",@"0",@"0",@"0",@"0",@"0",@"0",nil];
y=[[NSMutableArrayalloc] init];
y=[NSMutableArrayarrayWithObjects:@"0",@"0",@"0",@"0",@"0",@"0",nil];
    }
returnself;
}
@end

Then i used to call them and re,ove and added elements using the following code….

[[shared sharedInstance].y removeLastObject];
[[shared sharedInstance].y insertObject:new_element atIndex:0];

[[shared sharedInstance].x removeLastObject];
[[shared sharedInstance].x insertObject:new_element atIndex:0];

In the mean time i call these values and calculate an arithmetic value using an expression.

This seems to work well. But it seems to be an inefficient way to handle floating point numbers which i store in it. As these arrays creates objects. Is there any easy method that i can create a global array containing specified amount of floating point numbers and update it during the run of the programm(array size is fixed) by deleting the last object, and call them back to do calculation?

Please help me!

EDIT 1
To sir deanWombourne
……………………………
I implement as you instructed! Can you please go through this and help me to correct 2 errors i get.

IN the .h file

@interface Shared : NSObject{
@private
float input[7];
float output[6];

}
+(Shared*)sharedInstance;

-(void)addNewInput:(float)input1;
-(float *)input;
-(void)addNewOutput:(float)output1;
-(float *)output;

@end

in .m file…………

@implementation Shared

-(id)init{
if((self =[superinit])){

for(int n=0; n<7 ;++n)
input[n]=0.00f;

for(int n=0; n<6 ;++n)
output[n]=0.00f;
    }
returnself;
}

-(void)addNewInput:(float)input1{
input[0]=input[1];
input[1]=input[2];
input[2]=input[3];
input[3]=input[4];
input[4]=input[5];
input[5]=input[6];
input[6]=input1;
}

-(float *)input {
returninput;
}


-(void)addNewOutput:(float)output1{
output[0]=output[1];
output[1]=output[2];
output[2]=output[3];
output[3]=output[4];
output[4]=output[5];
input[5]=output1;
}

-(float *)output {
returnoutput;
}
@end

When calling it

float reading=  (accel_reading)/(1.165969038*1e5f);
[[SharedsharedInstance] addNewInput:reading];

Problems i get
1. In the implementation, it says incomplete implementation (it’s a warning not an error)
2. How can i used a for loop to fill array values or is this way ok?

Major problem i get,
When i call it as shown above, program stops running telling
Terminating application due to uncaught exception ‘NSInvalidArgumentException’, reason ‘+[SharedsharedInstance]: unrecognized selector sent to class 0x5780’

Please help me through this……………

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:15:01+00:00Added an answer on June 4, 2026 at 7:15 pm

    Your code Smells (and I mean that in the nicest possible way!)

    Using two parallel arrays and keeping in sync is a bad design pattern (and a performance hit in quite a few ways!). Especially as there is already a struct that handles storing an x and y at the same time – CGPoint).

    You’re solving the ‘only objects go in arrays‘ problem by converting your float' primitives toNSString` objects, which is horrendously inefficient – take a look instead at the NSValue class, it’s designed to put native C primitives into an object without expensive parsing operations 🙂

    You might also want to look into malloc (and free etc) and deal with the whole problem at the C level – this will mean no objects at all and would be blindingly fast (at the cost of more complicated code).

    Hope this helps, if you have any questions just add a comment to this answer 🙂


    EDIT

    If all you want to do is store 4 x and y values, then this is probably the easiest way to do it :

    @interface Shared : NSObject {
    @private
        CGPoint points[4];
    }
    
    +(Shared *)sharedInstance;
    
    - (void)addNewPoint:(CGPoint)point;
    - (CGPoint *)points;
    
    @end
    
    @implementation
    
    - (id)init {
        if ((self = [super init])) {
            // Start with 0,0 for all your points
            for (int n = 0; n < 4; ++n)
                points[n] = CGPointZero;
        }
        return self;
    }
    
    - (void)addNewPoint:(CGPoint)point {
        // Just move all the points along one and add the new one to the end
        // (yes, this could be done in a loop but there's not that much point for 4 points!)
        points[0] = points[1];
        points[1] = points[2];
        points[2] = points[3];
        points[3] = point;
    }
    
    - (CGPoint *)points {
        return points;
    }
    
    @end
    

    This gives you a method addNewPoint that removes the first point and adds the new point to the end of your array.

    You also get the method points that returns the 4 points. Use it something like :

    // To add a point
    CGPoint newPoint = CGPointMake(100, 100);
    [[Shared sharedInstance] addNewPoint:newPoint];
    
    // To do something with the points (in this case, NSLog them)
    CGPoint *points = [[Shared sharedInstance] points];
    for (int n = 0; n < 4; ++n)
        NSLog(@" Point %i : %@", n, NSStringFromCGPoint(points[n]));
    

    EDIT #2

    From your comments, you need two arrays, one with input data and one with output data. Try something like this :

    @interface Shared : NSObject {
        float inputs[4];
        float outputs[5];
    }
    ...
    

    This will give you two arrays to read/write to – one called inputs and the other called outputs. Access them in pretty much the same way you did the ones in my first edit :

    float *inputs = [[Shared sharedInstance] inputs];
    for (int n = 0; n < 4; ++n)
        NSLog(@" Input %i : %f", n, inputs[n]);
    
    float *outputs = [[Shared sharedInstance] outputs];
    for (int n = 0; n < 5; ++n)
        NSLog(@" Output %i : %f", n, output[n]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to create one js file which includes every js files to attach
I wanted to create an new gesture keyboard which can be used by all
I wanted to create Custom Dialog like the following image which can be called
I wanted to create smth similar to this one Kansas county map where user
I wanted to create a page with a simple button which runs away from
I wanted to create an HTML page which would have a set search criterias.
I wanted to create a broadcastreceiver which listens for android.intent.action.MEDIA_BUTTON, and get the extra_key_event
I wanted to create a counter that updates by one, every time it satisfied
I wanted to create a search add-on to right click and search tag text
I wanted to create a new property on a table in my model.. Basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.