Solved

problem concerns "for" command

Posted on 2004-04-18
10
173 Views
Last Modified: 2010-04-01
Here it is:

for( i = 0 ; i <= 11 ;  i++)
a[i][0];

what i wanted to do is to find an algorithm that repeats three times the first values of the array:

e.g
a[0][0],a[0][0],a[0][0]
a[1][0],a[1][0],a[1][0]
a[2][0],a[2][0],a[2][0]
             .
             .
             .
instead of a[0][0],a[1][0],a[2][0]

please help

0
Comment
Question by:daemian
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:monkesdb
ID: 10855308
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
    a[i][0];
0
 

Author Comment

by:daemian
ID: 10855872
Ok but let's make it a bit more difficult:

how about if we have something like this:

temp1 = c[f][k] * a[i][0];

and we want to do what i mentioned in just the a[i][k].How can we do that so as the c[f][0] won't change?
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 10857355
you aren't making any sence!!

write out the full thing
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10857430
Hi daemian,
> how about if we have something like this:
>
> temp1 = c[f][k] * a[i][0];
>
> and we want to do what i mentioned in just the a[i][k].How can we do
> that so as the c[f][0] won't change?

Be more abstract. Do you want to do matrix/vector multiplications? Or what do you want to do?

Cheers,
Stefan
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:daemian
ID: 10858514
well here it is the exact code of what i am trying to do:
------------------------------------------------------------------------
for (i = 0; i <= 2; i++)  
{
temp1 = 0;
for (f1=0,f2=2; f1 <= 9,f2 <= 11; f1=f1+3,f2=f2+3)
for (k = 0 ,f = f1;k <=2 , f <= f2;k++ , f++)
{
ifs >> a[i][k];  
in_stream1 >> c[f][0];
temp1 = a[i][k] * c[f][0];
out_stream1 << temp1 <<' ';
}  
}
out_stream1 << endl;
cout << endl;
-----------------------------------------------------------------------

I want to be able to multiply array a[i][k] with array c[f][0].
the first array contains:
1 2 3
3 4 5
5 6 7

and the second array contains :
1
3
2
5
4
2
3
5
4
2
3
1

so i would like array a to be able to multiply with the first three numbers of array c three times.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10859670
daemian,
Hmmm...
        for (i = 0; i <= 2; i++) {
                temp1 = 0;
                for (f1 = 0, f2 = 2; f1 <= 9, f2 <= 11; f1 = f1 + 3, f2 = f2 + 3)
                        for (k = 0, f = f1; k <= 2, f <= f2; k++, f++) {
                                ifs >> a[i][k];
                                in_stream1 >> c[f][0];
                                temp1 = a[i][k] * c[f][0];
                                out_stream1 << temp1 << ' ';
                        }
        }
        out_stream1 << endl;
        cout << endl;

You're using zillions of variables which are not necessary. And your second array is one-dimensional, right?

        for (a_row = 0; a_row <= 2; a_row++) {
                for (vec_group = 0 ; vec_group <= 9 ; vec_group += 3)
                        for (a_col = 0, vec_idx = vec_group; a_col <= 2, vec_idx <= vec_group+2; a_col++, vec_idx++) {
                                ifs >> a[a_row][a_col];
                                in_stream1 >> c[vec_idx];
                                temp1 = a[a_row][a_col] * c[vec_idx];
                                out_stream1 << temp1 << ' ';
                        }
        }
        out_stream1 << endl;
        cout << endl;

It's still not clear to me what you're doing there. Especially that you're reading a[x][y] over and over again.

Stefan
0
 

Author Comment

by:daemian
ID: 10867363
thanks for the info stefan!
With a combination of your code and mine i finally managed to find a solution(almost).Just a final question and these points are yours:

i wrote this:

for(a=0;a<=2;a++)
{
for(b=0;b<=2;b++)
{
infile >> c[b][0];
cout << c[b][0];
}

but it reads something like this:
c[0][0]=1
c[0][0]=3
c[0][0]=2

should it possibly be the same number like this?
c[0][0]=1
c[0][0]=1
c[0][0]=1
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 11227178
PAQed, with points refunded (200)

GhostMod
Community Support Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now