-1

The question: Modify the first Loop so that the values in the array data are initialized to multiples of 2 as
follows: 2,4,6,....,20.

here is the original codes that has been set in the question paper:
#include <stdio.h>
#define numitems 10
int main(int argc, char* argv[])
{
int data[numitems];
int i;
int *ptr;
for(ptr = data; ptr < &data[numitems]; ptr++)
*ptr = 0;
for (i=0;i<numitems;i++)
printf("data[%d]=%d\n", i, data[i]);
return 0;
}
HERE ARE MY MODIFICATION BUT STILL NOTHING IS BEING DISPLAYED:
    #include <stdio.h>
#define numitems 10
int main(int argc, char* argv[])
{
int data[numitems];
int i;
int *ptr;
for(ptr = data; ptr < &data[numitems]; (ptr=ptr+2)){
*ptr = 2;
}
for (i=0;i<numitems;i++)
{
printf("data[%d]=%d\n", i, data[i]);
}
return 0;
}
Comments
You mean you are in an exam?
4
Contributors
3
Replies
19
Views
2 Years
Discussion Span
Last Post by David W
1

Hi,

here is the original codes that has been set in the question paper

Is it permitted in your school to share and ask for help on a question paper, that is not an assignment?

0

^^ good point.
If it is, we can only give advice on how to solve this problem, not a direct solution or examples.

0

When you begin computer programming ... don't leave your 'normal thinking skills' ... behind.

How is the series 2,4,6,8, ...
related to        1,2,3,4, ..

.

Answer: ?

How could you design a program for this?

Or ... if you start with 2
What can you keep adding to the previous value(s) ...
So that you get ... 4,
                    6,
                    8,
                    ... etc

Can you code this in C?

Edited by David W

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.