473,378 Members | 1,344 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,378 software developers and data experts.

Parallel arrays

momotaro
357 100+
am trying to count the accurence and the position of dots in an array and fill another array with the locations found:
this is the code:
Expand|Select|Wrap|Line Numbers
  1. void get_dot_pos(char *IP, int *pos)
  2. {
  3.     int i = 0, j, cnt = 0;
  4.     while(i < 15)
  5.     {
  6.         if(IP[i] != '.')
  7.             i++;
  8.         else
  9.         {
  10.             for(j = 0; j < 3; j++)
  11.             {
  12.                 cnt++;
  13.                 pos[j] = i;
  14.             }
  15.         }
  16.     }
  17. }
  18.  
Aug 3 '07 #1
3 1750
momotaro
357 100+
this one works better but still not that good!
am having the same value for pos[0] pos[1] pos[2]

Expand|Select|Wrap|Line Numbers
  1. void get_dot_pos(char *IP, int *pos)
  2. {
  3.     int i = 0, j = 0, cnt = 0;
  4.     while(i < 15)
  5.     {
  6.         if(IP[i] != '.')
  7.             i++;
  8.         else
  9.         {
  10.             cnt++;
  11.             pos[j++] = i;
  12.         }
  13.     }
  14. }
  15.  
Aug 3 '07 #2
momotaro
357 100+
final version 100% working

Expand|Select|Wrap|Line Numbers
  1. void get_dot_pos(char *IP, int *pos)
  2. {
  3.     int i = 0, j = 0, cnt = 0;
  4.     while(i < 15)
  5.     {
  6.         if(IP[i] != '.')
  7.             i++;
  8.         else
  9.         {
  10.             cnt++;
  11.             pos[j++] = i;
  12.             i ++;
  13.         }
  14.     }
  15. }
Aug 3 '07 #3
JosAH
11,448 Expert 8TB
final version 100% working

Expand|Select|Wrap|Line Numbers
  1. void get_dot_pos(char *IP, int *pos)
  2. {
  3.     int i = 0, j = 0, cnt = 0;
  4.     while(i < 15)
  5.     {
  6.         if(IP[i] != '.')
  7.             i++;
  8.         else
  9.         {
  10.             cnt++;
  11.             pos[j++] = i;
  12.             i ++;
  13.         }
  14.     }
  15. }
Good! You did it all yourself with no help from here. You found your first solution
and if you have enough time you can notice that:

1) j and cnt increment simultaneously, i.e. both have the same values all the time;
2) i increments at every loop pass, no matter the if- or the else-part.

so, you can get rid of either j or cnt and you can simplify that loop somewhat:

Expand|Select|Wrap|Line Numbers
  1. int cnt, i;
  2. for (i= cnt= 0; i < 15; i++)
  3.    if (IP[i] == '.')
  4.       pos[cnt++]= i;
  5.  
This is called 'micro-optimization' and you should stay far from it or as late as
possible; only apply this when your program/algorithm works 100% as in your
case. Micro-optimizing it later is easy; coming up with a working solution is the
hard part.

kind regards,

Jos
Aug 3 '07 #4

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

Similar topics

6
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within...
12
by: lifeshortlivitup | last post by:
I am trying to construct two, one-dimensional parallel arrays that will give me a letter grade for whatever score I enter. The two arrays I need are minimum score and grade. I don't understand how...
9
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the...
8
by: earla12 | last post by:
Hi, I'm working on a program that takes the following input from the user: Student Name, Student Number, and Student GPA The information is then supposed to be stored in three separate...
2
by: dejavu33 | last post by:
Hello! Im learning arrays and am having trouble with parallel arrays. I dont understand exactly what they are. Can anyone give me an explanation of what they are or maybe how they can be declared...
3
by: darklunar | last post by:
Hello I've been given this assignment where I am to read from a text file of a number of students with a list of their names and GPA. Here is the text: James 3.9 Margaret ...
4
by: Soren | last post by:
Hi, I want to control some motors using the parallel port.. however, my laptop does not have any parallel ports (very few do). What I do have is a USB->Parallel converter... I thought about...
1
by: joor | last post by:
Hi I am a beginner and currently trying to create a small program. I seem to be stuck on the multiplication of their elements. Eg. I have an Array with 4 different prices for 4 different...
4
by: mh | last post by:
I want to interate over two arrays in parallel, something like this: a= b= for i,j in a,b: print i,j where i,j would be 1,4, 2,5, 3,6 etc.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...

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.