• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

create a table of integers but respecting oneness of its elements

Salma

Member
i think from the title you will get what i mean
my code is the following one ..but i need your help to see if the logic i used is correct:

#include<stdio.h>
#include<stdlib.h>

int main()
{
int i,j,t[5];
printf("enter the first element:\n");
scanf("%d",&t[0]);
for(i=1;i<4;i++)
{ printf("enter the element:\n");
scanf("%d",&t);
for(j=0;j<i;j++)
if(t[j]==t)
{printf("retype an other integer:\n");
scanf("%d",&t);

}
}
for(i=0;i<4;i++)
printf("%d",&t);
return 0;

}




cordially
 
Oneness?

A few things I noticed right away. You didn't allocate any memory for your array t[5]. You will only prompt the user to retype their number once, what if they keep on entering bad numbers?

Some tips, when you prompt for input, store the inputted number into a temporary variable. From there, loop through the array t checking to make sure the number doesn't already exist, if it does just keep on repeating until they enter a proper number. A while loop will help you with this. After you have looped through and detected that the input is proper, then store it in the appropriate place in your t[5] array.

One last thing, your array has spots for 5 numbers, but all of your loop controls stop at 4 numbers. Remember that C arrays are 0-based indexes. So if it has 5 elements, you have indexes 0-4. So your loops should read i <= 4if you want to use all spots in the array.




 
He doesn't need to allocate any memory for the array, since it's a local scope lvalue and will be on the stack. Agreed with the comments about the array bounds. Noticed that too. And what about...

if(t[j]==t)

t is the name of the array t[], and so will resolve to the address of the first element in the array. I'm guessing this is not what was intended.
 
Originally posted by: Markbnj
He doesn't need to allocate any memory for the array, since it's a local scope lvalue and will be on the stack. Agreed with the comments about the array bounds. Noticed that too. And what about...

if(t[j]==t)

t is the name of the array t[], and so will resolve to the address of the first element in the array. I'm guessing this is not what was intended.

Ah that's right. I should force myself to write stuff in C/C++ just to keep fresh on that stuff 🙁
 
Originally posted by: Crusty
Originally posted by: Markbnj
He doesn't need to allocate any memory for the array, since it's a local scope lvalue and will be on the stack. Agreed with the comments about the array bounds. Noticed that too. And what about...

if(t[j]==t)

t is the name of the array t[], and so will resolve to the address of the first element in the array. I'm guessing this is not what was intended.

Ah that's right. I should force myself to write stuff in C/C++ just to keep fresh on that stuff 🙁

I'd be happy to forget about it forever, except that people keep coming here and asking questions 🙂. I look forward to the day when someone asks me a question about a pointer, and I honestly can't remember what one is.
 
Looks like the forum eats stuff in square brackets ... still, the OP doesn't appear firm with loops and arrays.

Let's have the OP do his/her own homework, shall we?
 
Originally posted by: Peter
Looks like the forum eats stuff in square brackets ... still, the OP doesn't appear firm with loops and arrays.

Let's have the OP do his/her own homework, shall we?

That's why I didn't give him any code, just some pointers :laugh:!
 
I have no problem with people posting code they have written for assignment, as long as they have done the initial work and want help. It's when they just post the problem statement out of the book that I get irritated.
 
My thoughts as a Senior Moderator

Homework assistance is acceptable under the following guidelines - These are not firm rules, but seem to be acceptable by the S/W community here.

1) It is admitted to be homework

2) It is not a request for last minute help

3) Initial thought has been presented by the OP

4) Hopefully, people posting assistance will not provide the complete work for them.


Perps that do not learn how to do their own work are not the type I want designing/programming equipment that my life may depend on.

Markbnj seems to have the same mind - he is one reason that I am able to relax a little around the S/W forums.:thumbsup:
 
Originally posted by: Common Courtesy

Perps that do not learn how to do their own work are not the type I want designing/programming equipment that my life may depend on.

I wouldn't worry about those people too much, if they can't figure out how to do homework they most likely they can't even fill out a college application 😉
 
Originally posted by: tfinch2
I'm still trying to figure out what "respecting oneness of its elements" means? 😕

I still don't know what he means by it, but judging from the code I'm guessing he wants the list of numbers to be unique.
 
Originally posted by: Crusty
Originally posted by: tfinch2
I'm still trying to figure out what "respecting oneness of its elements" means? 😕

I still don't know what he means by it, but judging from the code I'm guessing he wants the list of numbers to be unique.

Yeah, I took that to mean uniqueness as well.
 
Originally posted by: Crusty
Oneness?

A few things I noticed right away. You didn't allocate any memory for your array t[5]. You will only prompt the user to retype their number once, what if they keep on entering bad numbers?

Some tips, when you prompt for input, store the inputted number into a temporary variable. From there, loop through the array t checking to make sure the number doesn't already exist, if it does just keep on repeating until they enter a proper number. A while loop will help you with this. After you have looped through and detected that the input is proper, then store it in the appropriate place in your t[5] array.

One last thing, your array has spots for 5 numbers, but all of your loop controls stop at 4 numbers. Remember that C arrays are 0-based indexes. So if it has 5 elements, you have indexes 0-4. So your loops should read i <= 4if you want to use all spots in the array.

Rather then doing i <= 4 he should define a constant and do i < NUM_ELEMENTS. If you are going to be lazy at least use i < 5 so that a maintainer at least has some hope of noticing that both magic number should be the same.
 
Originally posted by: smack Down
Originally posted by: Crusty
Oneness?

A few things I noticed right away. You didn't allocate any memory for your array t[5]. You will only prompt the user to retype their number once, what if they keep on entering bad numbers?

Some tips, when you prompt for input, store the inputted number into a temporary variable. From there, loop through the array t checking to make sure the number doesn't already exist, if it does just keep on repeating until they enter a proper number. A while loop will help you with this. After you have looped through and detected that the input is proper, then store it in the appropriate place in your t[5] array.

One last thing, your array has spots for 5 numbers, but all of your loop controls stop at 4 numbers. Remember that C arrays are 0-based indexes. So if it has 5 elements, you have indexes 0-4. So your loops should read i <= 4if you want to use all spots in the array.

Rather then doing i <= 4 he should define a constant and do i < NUM_ELEMENTS. If you are going to be lazy at least use i < 5 so that a maintainer at least has some hope of noticing that both magic number should be the same.

#define NUM_ELEMENTS 5
int i,j,t[ELEMENTS ];
...

i < NUM_ELEMENTS


 
A minor point, but you're going to want to pass-by-value when calling printf in this case. Passing the address of t will make it literally print t's address. Try printf("%d\n",t);
 
Probably still broken. The formatting.

In regular Message posting dialog, you can use multiples of

& n b s p ; (take out the spaces between chars though)

in order to get some spacing off from the left.

class CircleList(list):

def __init__(self, aList):


Just don't edit that message though because it will strip out the spaces again!
 
Back
Top