Accessing multidimensional arrays without using loop? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi friends I was working on a project where we need to use quite a few multidimensional arrays. I am using for loops to access different array elements, then I just thought what if I don’t have a liberty to use looping? How am I going to access array element?
I am new to C, so thought of discussing here, I am sure there might be thousans of people who could have thought the same way, and hopefully found the solution.
Below example of multidimensional array is give, please guide me.
Thanks
static int t[3][2][4] = { {2,4,3,6,
1,6,7,9,},
{8,2,1,1,
2,3,7,3,},
{1,6,2,4,
0,7,9,5,},
};
Please Help me...thanks!

If you need to go through all of the values inside the loop without manual handling (i.e. x = t[1][1][1] then x = t[1][1][2] etc) then you want to use loops, enhanced loops or iterators. However since you're using C the only of those three options available are standard loops, which you're trying not to use. So... there's mo straight forward way to do that really.
If you're willing to use some other C libraries however then there may be more options for you. Iterator libraries probably exist.
A non-straightforward way to do it (if you're looking for one) could be through recursion, however that's really quite wasteful. I advise you just use loops :P

What are you trying to prove with loops and without loops should be first thought.
If u want to access all the elements and not use loop is like writing a lot of code manually and waste of memory(in your case 3 * 2 * 4 no of lines instead of few ).
Instead of showing the array if you had put in your code how and where you accessing elements it would have been more clear to tell what you wanted .

Related

Is there any way to assign properties to differentiate values which would otherwise be considered the same? (in C language) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Let me start by saying that I'm a beginner programmer that knows very basic C
The exact code I'm working on doesn't matter here its just that I've run into a very odd problem (I'm messing around with ASCII graphics)
I want to differentiate between two 'O' characters. I could just use different characters but I want it to look good. I tried using Unicode to differentiate them (since Latin O and Greek Omicron look the same) but it won't work if I send it to someone else (since my output is on command prompt) edit: won't work if I send it to someone since command prompt must be manually configured to allow unicode characters (atleast to my understanding)
I don't know if using structures would work and I want to write this in C and not any other OOP language like python (because I'm practicing C)
So is there any logic I could use to do this?
EDIT: I want to apply different behaviours to both O and there will be multiple O of each type on screen (ik this is what objects do but pls help me with a logic for C)
EDIT 2: Apparently my question is very vague so I'll elaborate
I'm having a 2d array that I'm using as a game screen
I want O to come in from each side (top bottom right left), scroll across the screen and exit from the other side
I already came up with the logic to make it work for a single side but the problem comes in when I want to make it work for all sides
My current logic can't differentiate between an O that has to go left and and O that has to go down
Hence why I want to different between two instances of O
(I haven't posted my code since I just need the logic and want to solve the actual code by myself and also it's probably very inefficient or convuluted etc)
tldr: I want to differentiate between "O" and "O"
My current logic can't differentiate between an O that has to go left and and O that has to go down
Your problem is that you don't differentiate internal representation and external presentation.
You have two distinct objects that move across the screen. They both look like an O character. This doesn't mean your program should store them both as 'O', some variation of 'O' such as the omicron character. The program can store e.g. numbers 1 and 2, or any two different objects that are convenient to work with, and display an O in place of either.

C - Passing a single variable instead of an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm facing an issue on a PIC from Microchip but my question is more general. This will help me check if it is an issue from my code or from the debugger of MPLAB X which is sometimes buggy.
So here is my question, let's say I have this function:
int test( int *array )
{
int a = array[0];
return a;
}
Now, if I want to pass an array it's working, there is no issue. But let's say I want to use a single variable instead of the array like this:
int main()
{
while(1)
{
int test_variable_1 = 8;
int test_variable_2 = test( &test_variable_1 );
}
return 0;
}
I did not use an array but for me this should work as expected since the test_variable_1 is like an array of size of one int.
EDIT QUESTION: So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function? Also, is it a good/bad practice?
Thanks in advance and don't hesitate to tell me if I'm unclear.
Have a good day!
Adrien
So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function?
Yes. In fact C doesn't even know that you passed an array. For details, see Do pointers support "array style indexing"?
Also, is it a good/bad practice?
It's fine. Though ideally the function should take the number of items as one of the parameters passed.

Shift a whole block in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I was wondering if there is a possibility to shift a whole block in an array. I intend to delete one item in an array of dynamic length, and after I deleted it, I want the whole block to shift to the right. So far I do this element, by element, but this is not efficient. So I was wondering if there is a better solution.
Important: the order of the elements needs to be kept the same.
Using memmove will probably be more efficient than doing an element by element copy as then you can take advantage of the fact you're doing a bulk move rather than lots of little moves (and compilers often provide highly optimized memmove implementations), but that's about it. You need to move everything around in memory, so you're going to have to move it.
If you're doing this a lot with your arrays, it probably means you need a different data structure.
You can use linked list instead of array. In linked list deleting is easy. You just have to point the next next elements.
Use a ring buffer, like this:
unsigned idx;
int Buf[1024];
void EnterValue(int value)
{
idx++;
Buf[idx & 1023] = value;
}
int GetOldValue(unsigned age)
{
assert(age<1024);
return Buf[ (idx-age) & 1023 ];
}

C guessing game between two players with visual output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have been given the assignment to create a quite complicated to me, but simple to some C game. The program will run and generate 25 random numbers between 1 to 100 (no repeats). The game is between two users.
Both the players will get two guesses each time. Every correct guess will be counted as 1 correct answer and will be displayed on the screen in the board.
The player having more number of correct guesses will be a winner.
The game will continue until the board is completely revealed.
The first screen should ask the user for his name, print a welcome message, and displays an empty 5x5 board. But this 5x5 board has the values in it internally
Now the program should ask the number of players (1 or 2)
If one, ask one name and second is computer and if 2 ask two names
For playing against computer, you are asked for two guesses and every correct guess is shown on the board.
Now 2 guesses for computer would be taken and shown on the screen.
I have tried everything to my knowledge to complete this but I lack a full understanding of C. Any help would be appreciated. I did not include my code because, honestly its just a mess and does not even run.
I would like to see someone be able to make such a game, so that I can study the logic. NOT COPY THE WORK
Simple steps. Start small and grow.
Easy one is to remove all questions to the user(s). Hard code the answers in the program. You can retrofit the IO later.
Start with a way ot generating 25 random numbers and load them into an array. Place the array in a global variable. You need a another array to show when a number is sucessfully guessed.
Now write a function to display that guessed array as 5X5.
Gradually build the program up
Always make functions
generate_array
show_guessed
....
If get stuck on specific things then post a new question.
This is not a direct answer, but a very long comment with some requests for more information and effort.
The problem I have with your request is that I see zero effort. I see a request for teh C0d3z and a promise not to cheat after receiving something that makes it far too easy to cheat.
What I, and probably others here, want to see is effort and some attempt.
Do you know how to print text to the screen like you see in the requirements? Do you know how to print text at all? If so, make the print routines and state this.
Do you know how to generate random numbers? If so, say so in your question and we see effort.
Do you know how to receive input from the user?
Do you know what an array is?
Do you know how to save code in your editor and compile it?
You can see that without any sort of background or starting code, we don't know where you are in your learning.
So...
Post an attempt at solving your problem in your question. If you are truly so new to coding that you cannot do this, then you need to sit down with your professor / TA / whomever and tell them this. Reading through your teaching material (textbook or whatever) should help a whole lot as well. This assignment feels like something I would see towards the end of a beginner's C class. If this is where you are, and you really have no clue what to do, then you may need to retake the class.

High level planning of when to use pointers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So pointers are something a lot of people (including myself) get a little confused on. I understand how they work, but when im making an application or whatever....how do I know when it would be best to use them?
Is there like a general rule/guideline of when it's actually a good idea to use them. I feel like I understand their concept for the most part, but always struggle in wondering if I should be using a pointer here or there etc...
Thank you.
They are a basic part of the C language. Without being rude, I think the key thing to consider is that they are not something to be avoided. They are another tool at your disposal, and a powerful one at that.
In general, they are used when you want to access large amounts of data (ie: arrays, massive structures, dynamic memory allocation, etc). They are also very useful when designing APIs (application programming interfaces). This shouldn't really be C# question, as in C# and C++, you can pass by value or by reference, and the nature of your question changes based on the usefulness of passing by value or reference, along with the user of other means of accessing data external to a function, class, etc.
Pass by Reference / Value in C++
If you can just pass by value, and the value is read-only, you're fine without a pointer, as you have no means to change the value. A common case is a simple sum function:
int sum (int a, int b) {
int value = a + b;
return value;
}
You have no need to modify the values provided, and you're only accessing simple values, rather than arrays or complex structures.
Now, if you're modifying the contents of a large array, especially if the size of the array can change at run time (ie: linked lists and other data structures) you need pointers, period. If you're modifying multiple values, you could either use pointers, or you could just use the return value of a function (ie: int mysum = sum(1,2);). About the only concern I encounter when using pointers in regular work is making sure any possible NULL pointer values are checked for at the beginning of each function. The real problem is when working with larger projects where you can get non-NULL, invalid/corrupt pointers from a poorly written third party API or library. That's where the real fun begins, debugging with JTAG debuggers, GDB, etc.
For c, I think you will naturally end up using pointers for just about any but the most trivial of trivial programs. You will need a pointer when you want to refer to any non-basic type, e.g. structs, arrays (including strings).
Lots of other places too:
any time you do dynamic memory allocation (malloc() and friends)
if you need to return more than one "thing" from a function, you can pass a pointer to the storage for that object and have the function populate that memory
you might need to use function pointers
probably lots of other examples too.
Well, a couple of easy-bits would be, in case of dynamic memory allocation, you would want to use Pointers.
If you are not sure of the size of arrays that you are going to use, better implement a linked list.
Want multiple return values from a function, better pass the address of variables to be modified.
Any place, where you are not 100% sure, if you should be using a normal variable, use Pointer.

Resources