?
Solved

printing the different elements of 2 arrays

Posted on 2008-11-06
9
Medium Priority
?
190 Views
Last Modified: 2010-07-27
We have 2 arrays e.g.
int A[]={1,2,3,4,5,6,7,8,9,10},
     B[]={1,3,2,8,7,4,7,8,11,10};
I want to write a C program to print the different elements of the arrays in this way:
2: 2 3
down to
6: 6 4
9: 9 11
i.e. when we have continuous different elements,  print only the first and the last element of the sequence.
Thanks in advance
0
Comment
Question by:xiromdim
  • 4
  • 4
9 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 22900510
Is this homework?
what are you having difficulty with?
0
 

Author Comment

by:xiromdim
ID: 22900804
well, it is a part of a homework...I will post the code I have written so far...I know it is not so dificult but I'm a bit confused...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22902713
Also, what's the question ? In order for us to help you, we need to know what you need help with :) We can't do the work for you ...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:xiromdim
ID: 22907765
dear experts, I mean this: with the code below we take all the different elements of the 2 arrays. The output of this code is: 1: 2 3 \n 2: 3 2 \n 3: 4 8 and so on...
The question is how can I take this output:
1: 2 3 //when we have continuous diff. elements print only the first and the last
down to
5: 6 4
8:9 11

I hope , this time I have been more clear...
Jim
#include<stdio.h>
 
int main(){
    int A[]={1,2,3,4,5,6,7,8,9,10};
    int B[]={1,3,2,8,7,4,7,8,11,10};
    
    for(int i=0; i<10; i++){
            if (A[i]!=B[i]) 
               printf("%d: %d  %d \n",i, A[i], B[i]);
               }
    getchar();
    return 0;
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22907918
You mean you want to print only elements 1 through 8 rather than 0 through 9 ? If so, you just have to adjust the for loop's start point and upper limit.

If that's not what you mean, then I'm afraid I don't understand your question.
0
 

Author Comment

by:xiromdim
ID: 22908179
I mean: first of all we want to print only the different elements. This is very easy...I did it (by the way the homework is about txt files, so the code with the 2 arrays is NOT the homework...)
But, if we have continuous different elements (e.g. A[1],A[2],A[3],A[4],A[5]) I want to print only A[1] (the first) and A[5] (the last), with the message "down to" (or whatever...).
The problem is:
if (A[i]!=B[i]), I must keep track of the previous elements (equal or not-equal).
If A[i-1]==B[i-1] then startIndex=i, else (i.e. A[i-1]!=B[i-1]) there is a startIndex somewhere before.
Also: if A[i]!=B[i], I have to examine (or better keep track) the previous elements, to check if i is the last index of a sequence of indices with diff. elements, so lastIndex=i and so on....
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 22908203
You only need to keep the start index. Then you iterate over the array until you find equal values. And then you know the end index.
0
 

Author Comment

by:xiromdim
ID: 22908655
well I made it!!! here is the code... thanks...
#include<stdio.h>
 
int main(){
    int A[]={1,2,3,4,3,6,7,8,9,10};
    int B[]={1,3,2,8,7,4,7,8,11,10};
    int start, end, tempIndex;
    int i=0;
    
    while(i<10){
            if (A[i]!=B[i]){   printf("%d: %d %d \n",i, A[i], B[i]);//first diff. element
               start=i;
               tempIndex=i+1;
               while(A[tempIndex]!=B[tempIndex]){//go on while a[i]!=b[i]
                     tempIndex++;
               }
               end=tempIndex-1;
               if (end-start>1){
                                
                                printf("down to\n");
                                printf("%d: %d %d \n",end, A[end],B[end]);
                                }
               i=end+1;                 
               }//if a[i]!=b[i]
               else i++;
               }
    getchar();
    return 0;
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22908689
Looks good.

One thing though : properly aligning your code (consistently) will make it a lot easier to read and maintain.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question