I'm trying to do something that should be simple. I am making an array that only has 1 element in it. Which is coordinates x y z
So I figured Id make an array with 1 row and 3 columns, 1 column for the x y and z. I don't need anymore rows because this number is going to be constantly updated and I don't need memory of the previous point, so its just going to be constantly overridden
so here is my array
int coordinates[1][3] = {1,1,1};
and this is how I am trying to see if I built it correct and that the values print out.
printf("%d %d %d\n", coordinates[1][1],coordinates[1][2],coordinates[1][3] );
printf("\n");
My thinking that it will print Row 1 column 1, Row 1 Column 2, and row 1 column 3.
What is wrong with my thinking here? I'm not getting 1 1 1 as my result. I'm getting random numbers.
I'm not too familiar with C. This seem trivial and I'm starting to spin my wheels too much on it.
EDIT: MY PROCESS OF WORKING THINGS(Also see comment)
"This was the first thing I tried. Thinking back to arrays 101 and indexing is n-1 because It seemed like I was getting elements in random memory address outside the arrays bounds. Still did not work unfortunately im starting to beat my head against a wall."
I did try
printf("%d %d %d\n", coordinates[0][0],coordinates[0][1],coordinates[0][2] );
printf("\n");
but again i got random numbers
CONCLUSION
Ok I finally figured out what I was doing wrong. You all were right. I was looking in the wrong spot of my code. Thank you for your prompt responses. What was happening was I Originally called my coordinates as a float in my code. I didnt copy and past it here so I called it an int here. SO when I was trying to printf with %d it was trying to print an int for a float causing the messed up results. I switched the %d to %f (which i was unaware how the printf worked like that i guess) and it worked!. Green horn mistake and im sorry for it! Thanks again for the help!
Remember that array index starts from 0 in C, this rule applies to multiple-dimensional arrays as well.
For an array int coordinates[1][3], its elements are coordinates[0][0], coordinates[0][1] and coordinates[0][2].
it should be indexed as cordinates[0][0], cordinates[0][1], cordinates[0][2] because array indexing usually starts at 0.
here cordinates[1][3], means row size is one but it is indexed using 0. similary columns are indexed from 0 to 3. i.e. 0 to size-1
It should be like this
printf("%d %d %d\n", coordinates[0][0],coordinates[0][1],coordinates[0][2] );
As the index starts from zero not one.
Related
I'm totally new here but I heard a lot about this site and now that I've been accepted for a 7 months software development 'bootcamp' I'm sharpening my C knowledge for an upcoming test.
I've been assigned a question on a test that I've passed already, but I did not finish that question and it bothers me quite a lot.
The question was a task to write a program in C that moves a character (char) array's cells by 1 to the left (it doesn't quite matter in which direction for me, but the question specified left). And I also took upon myself NOT to use a temporary array/stack or any other structure to hold the entire array data during execution.
So a 'string' or array of chars containing '0' '1' '2' 'A' 'B' 'C' will become
'1' '2' 'A' 'B' 'C' '0' after using the function once.
Writing this was no problem, I believe I ended up with something similar to:
void ArrayCharMoveLeft(char arr[], int arrsize, int times) {
int i;
for (i = 0; i <= arrsize ; i++) {
ArraySwap2CellsChar(arr, i, i+1);
}
}
As you can see the function is somewhat modular since it allows to input how many times the cells need to move or shift to the left. I did not implement it, but that was the idea.
As far as I know there are 3 ways to make this:
Loop ArrayCharMoveLeft times times. This feels instinctively inefficient.
Use recursion in ArrayCharMoveLeft. This should resemble the first solution, but I'm not 100% sure on how to implement this.
This is the way I'm trying to figure out: No loop within loop, no recursion, no temporary array, the program will know how to move the cells x times to the left/right without any issues.
The problem is that after swapping say N times of cells in the array, the remaining array size - times are sometimes not organized. For example:
Using ArrayCharMoveLeft with 3 as times with our given array mentioned above will yield
ABC021 instead of the expected value of ABC012.
I've run the following function for this:
int i;
char* lastcell;
if (!(times % arrsize))
{
printf("Nothing to move!\n");
return;
}
times = times % arrsize;
// Input checking. in case user inputs multiples of the array size, auto reduce to array size reminder
for (i = 0; i < arrsize-times; i++) {
printf("I = %d ", i);
PrintArray(arr, arrsize);
ArraySwap2CellsChar(arr, i, i+times);
}
As you can see the for runs from 0 to array size - times. If this function is used, say with an array containing 14 chars. Then using times = 5 will make the for run from 0 to 9, so cells 10 - 14 are NOT in order (but the rest are).
The worst thing about this is that the remaining cells always maintain the sequence, but at different position. Meaning instead of 0123 they could be 3012 or 2301... etc.
I've run different arrays on different times values and didn't find a particular pattern such as "if remaining cells = 3 then use ArrayCharMoveLeft on remaining cells with times = 1).
It always seem to be 1 out of 2 options: the remaining cells are in order, or shifted with different values. It seems to be something similar to this:
times shift+direction to allign
1 0
2 0
3 0
4 1R
5 3R
6 5R
7 3R
8 1R
the numbers change with different times and arrays. Anyone got an idea for this?
even if you use recursion or loops within loops, I'd like to hear a possible solution. Only firm rule for this is not to use a temporary array.
Thanks in advance!
If irrespective of efficiency or simplicity for the purpose of studying you want to use only exchanges of two array elements with ArraySwap2CellsChar, you can keep your loop with some adjustment. As you noted, the given for (i = 0; i < arrsize-times; i++) loop leaves the last times elements out of place. In order to correctly place all elements, the loop condition has to be i < arrsize-1 (one less suffices because if every element but the last is correct, the last one must be right, too). Of course when i runs nearly up to arrsize, i+times can't be kept as the other swap index; instead, the correct index j of the element which is to be put at index i has to be computed. This computation turns out somewhat tricky, due to the element having been swapped already from its original place. Here's a modified variant of your loop:
for (i = 0; i < arrsize-1; i++)
{
printf("i = %d ", i);
int j = i+times;
while (arrsize <= j) j %= arrsize, j += (i-j+times-1)/times*times;
printf("j = %d ", j);
PrintArray(arr, arrsize);
ArraySwap2CellsChar(arr, i, j);
}
Use standard library functions memcpy, memmove, etc as they are very optimized for your platform.
Use the correct type for sizes - size_t not int
char *ArrayCharMoveLeft(char *arr, const size_t arrsize, size_t ntimes)
{
ntimes %= arrsize;
if(ntimes)
{
char temp[ntimes];
memcpy(temp, arr, ntimes);
memmove(arr, arr + ntimes, arrsize - ntimes);
memcpy(arr + arrsize - ntimes, temp, ntimes);
}
return arr;
}
But you want it without the temporary array (more memory efficient, very bad performance-wise):
char *ArrayCharMoveLeft(char *arr, size_t arrsize, size_t ntimes)
{
ntimes %= arrsize;
while(ntimes--)
{
char temp = arr[0];
memmove(arr, arr + 1, arrsize - 1);
arr[arrsize -1] = temp;
}
return arr;
}
https://godbolt.org/z/od68dKTWq
https://godbolt.org/z/noah9zdYY
Disclaimer: I'm not sure if it's common to share a full working code here or not, since this is literally my first question asked here, so I'll refrain from doing so assuming the idea is answering specific questions, and not providing an example solution for grabs (which might defeat the purpose of studying and exploring C). This argument is backed by the fact that this specific task is derived from a programing test used by a programing course and it's purpose is to filter out applicants who aren't fit for intense 7 months training in software development. If you still wish to see my code, message me privately.
So, with a great amount of help from #Armali I'm happy to announce the question is answered! Together we came up with a function that takes an array of characters in C (string), and without using any previously written libraries (such as strings.h), or even a temporary array, it rotates all the cells in the array N times to the left.
Example: using ArrayCharMoveLeft() on the following array with N = 5:
Original array: 0123456789ABCDEF
Updated array: 56789ABCDEF01234
As you can see the first cell (0) is now the sixth cell (5), the 2nd cell is the 7th cell and so on. So each cell was moved to the left 5 times. The first 5 cells 'overflow' to the end of the array and now appear as the Last 5 cells, while maintaining their order.
The function works with various array lengths and N values.
This is not any sort of achievement, but rather an attempt to execute the task with as little variables as possible (only 4 ints, besides the char array, also counting the sub function used to swap the cells).
It was achieved using a nested loop so by no means its efficient runtime-wise, just memory wise, while still being self-coded functions, with no external libraries used (except stdio.h).
Refer to Armali's posted solution, it should get you the answer for this question.
SO I was reading a book on C and came across a piece of code which I couldn't fully understand, so I decided to run it out and see the result for myself. Basically, I understand that an array variable acts like a pointer, in the sense, that it points to the first element of the array, for instance.
char quote[]="This is a nice cookie!"
Here quote which is an array variable, is basically a pointer to the first element of the array i.e. the letter T(Hope I am correct so far).
Building on this, when I write the below piece of code, I understand the following points.
At the declaration time, contestants is an array with 3 members in it - 1, 2 and 3
Then I use *choice as an array variable which in other words is a pointer to the first element of array contestants- which is now (1).
Now I begin dynamically assigning values to the members of the contestants array.
contestansts[2]=*choice
puts the value 0 in the 3rd member of the array i.e.: contestants[2].
So far, so good.
Now, the next for loop was not there in the book. I included it in the code myself, to see what are the elements of the array. When I run the code, as an output I get-
I will pick contestant number 2
The members of the array contestants are:
Member 0 has the value 2
Member 1 has the value 3
My question is- I do not understand how the member 1 has the value 3. According to the code, shouldn't members 1 and 2 both have the same value(2).
Any help would be highly appreciated.
I am using Sublime Text and the Mac terminal to run this(if needed).
#include<stdio.h>
int main()
{
int contenstants[]={1,2,3};
int *choice=contenstants;
contenstants[0]= 2;
contenstants[1]=contenstants[2];
contenstants[2]=*choice;
printf("\n I will pick contestant number %i\n", contenstants[2] );
//Print array members
printf(" The members of the array Contenstants are:\n");
for(int i=0;i<contenstants[2];i++)
{
printf(" Member %i\t has the value %i\t", i, contenstants[i] );
printf("\n");
}
return 0;
}
int contenstants[]={1,2,3};
int *choice=contenstants; // choice -> {1,2,3}
contenstants[0]= 2; // choice -> {2,2,3}
contenstants[1]=contenstants[2]; // choice -> {2,3,3}
contenstants[2]=*choice; // choice -> {2,3,2}
The output you get is totally logical!
In the first step you assign the value 2 to contenstants[0], which is also *choice.
The reason why you are only getting two numbers on the output is, that you loop for(int i=0;i<contenstants[2];i++), which is equivalent to for(int i=0;i<2;i++), which loops over element number 0 and element number 1.
It is 3 because you are doing
contenstants[1]=contenstants[2];
and then contenstants[1] is not modified later so it has value 3;
To answer your question in comment:
Condition in your for loop is not correct
--------------v not correct
for(int i=0;i<contenstants[2];i++)
You want
for(int i=0;i<3;i++)
Although using hardcoded 3 is also not a good idea. But it goes with your other code.
Hey all I am trying to store a matrix in an array of chars and then print it out.
My code that I have written:
#include<stdio.h>
#include<stdlib.h>
int main() {
int i;
int j;
int row=0;
int col=0;
int temp=0;
char c;
int array[3][2] = {{}};
while((c=getchar()) !=EOF && c!=10){
if((c==getchar()) == '\n'){
array[col++][row];
break;
}
array[col][row++]=c;
}
for(i=0; i<=2; i++){
for(j=0; j<=3; j++){
printf("%c ", array[i][j]);
}
printf("\n");
}
}
Using a text file such as:
1 2 3 4
5 6 7 8
9 1 2 3
I would like to be able to print that back out to the user, however what my code outputs is:
1 2 3 4
3 4 5 6
5 6 7 8
I cannot figure out what is wrong with my code, some how I am off an iteration in one of my loops, or it has something to do with not handling new lines properly. Thanks!
A few problems that I can see are:
As user3386109 mentioned in the comments, your array should be array[3][4] to match the input file.
The line array[col++][row]; does nothing but increment col, and then uselessly indexes the array and throws away the value. You can do the same thing with just col++;. However, you're not even using col at any later point in the code, so really you don't even need that. The break; all by itself does what you need. Which leads me to...
You're not populating the array like you think you are. You're incrementing col and then immediately breaking out of the loop. So how does the entire array ever get populated? Just by pure luck. As it turns out with your array declared as array[3][4], the array access array[0][4] (which isn't even technically supposed to exist) is equivalent to array[1][0]. This is because all multidimensional arrays (in C and just about any other language) are laid out in memory as flat arrays, because memory itself uses linear addressing. In C, this flattening of multidimensional arrays is done in so-called Row-major order, meaning that as you traverse the raw memory from first address to last, the corresponding multidimensional indices (i,j,k,...z, or in your case just i,j) increment in such a way that the last index will change the fastest. So, not only does col never get incremented except for right before you break out of the loop, but row never gets reset to 0, which means you're storing values in array[0][0], array[0][1], ... array[0][11], not array[0][0] .. array[0][3], array[1][0] .. array[1][3], array[2][0] .. array[2][3] as you were expecting. It was just luck that, thanks to row-major ordering, these two sets of indices were actually equivalent (and C doesn't do array bounds checking for you because it assumes you're doing it yourself).
This is just personal preference, but you will usually see arrays referenced as array[row][col], not array[col][row]. But like I said, that's just preference. If it's easier for you to visualize it as [col][row], then by all means do it that way. Just make sure you do it consistently and don't accidentally switch gears midway through your code to doing [row][col].
Your code will break and only print out part of the matrix if you accidentally put a trailing space at the end of one of your rows of numbers, because of the weird way you're checking for the end of input (doing a second getchar after each initial getchar and checking to see if the second character is \n). This method isn't wrong per se, in the sense that it will work, but it's not very robust and relies on your input data being precisely formatted and containing no trailing spaces. Anyone who has ever spent hours trying to figure out why their Makefile didn't work, only to find out that it was because they had leading spaces instead of tabs can attest to the fact that those kinds of errors can be extremely time-consuming and frustrating to track down. Precisely formatted input data is always a good thing, but your code shouldn't break in unexpected an non-obvious ways (such as only printing out half of a matrix) when it doesn't get perfect input. Edit: It only occurred to me later on that you were actually intending to do two mutually exclusive things here: increment col for the next line of input, and break out of the loop after having (presumably) detected the end of input. You need to figure out which thing you're doing here, although thanks to item #3, your code actually (and oddly) works just by taking user3386109's advice and changing array[3][2] to array[3][4].
I can only assume you used <= 2 and <= 3 in your for loops instead of < 3 and < 4, respectively, because you prefer doing it that way. That's fine, but it generally makes for easier-to-read code if your for loop conditions match up with your array dimensions. Just speculating here, but perhaps that's why you had array[3][2] when you really meant array[3][4].
So what I have is an array that's size is decided by me and then the elements in the array are randomly generated. It's supposed to take an integer array,its size, and an integer number
and find how many times the number is present in the array and return that count at the end.I keep trying stuff and nothing seems to be getting me anywhere close to an answer. I was just trying to see if someone could point me in the right direction on where to start
count_numbers(int array[], int size, int z)
Hhave you tried running a loop through the array and trying a match expression to the array value in another loop. This seems like a logic question rather than actual code related. Maybe a search around the internet looking at how to count in arrays could help you.
This should point you in the right direction...
for (int i = 0; i < arraySize; i++) {
if (array[i] == z /*z being your search value**/) {
you may have to alter this a little
//dosomething
// e.g. increment a count here
}
else
do-nothing essentially.
There is a method for checking array size - so don't worry about defining it's size. have a look at the java method for this and use it.
Hope this helps
I am new to stackoverflow as am new to programming, yet am not really a 'professional and enthusiast programmer'. Enthusiast maybe but not professional...
In a part of some beginner code of mine i have a two dimensional array diff[i][j], where the value is zero wherever i==j. I am trying to get the smallest value in each row but not the zero value...
the part of the code (under construction) that searches the smallest of the first row is:
i=1;
double smallest;
for ( j=1 ; j<=n ; j++ )
{
smallest = diff[i][j];
if ( j!=i && diff[i][j] < smallest )
smallest = diff[i][j];
}
printf("\n %lf\n", smallest);
however, the result is always the biggest number not the smallest. Anyone knows why??
P.S. I'd be thankful for any suggestion or comment of dealing with stackoverflow.com and the way i asked my question, since am new here... thank you in advance...
EDIT
after the answers below, i decided to make the i=1 a special case and make two separate functions for both cases... however, when i try to assign j to other variable i failed... in the previous code:
if (j!=i && diff[i][j]<smallest) {smallest=diff[i][j]; d=j}
declared d previously and everything... when i print d it prints a random number >maybe the memory location content... tried for debugging to assign an initial value - with the declaration - and when printing it came out the initial value... the point is i want d to hold the column where the smallest value is... how can i acheive that??
You never initialize smallest
i=1;
double smallest = diff[1][2]; // initialize it to a non-diagonal element in the column
for (j=1; j<=n; j++)
if (j!=i && diff[i][j]<smallest){
smallest=diff[i][j];
}
printf("\n %lf\n", smallest);
EDIT:
You also seem to have { smallest= diff[i][j]; .. } in your code that overrides the value of smallest each iteration. I removed it in my answer.
First thing, array indexes start from 0 in C, not 1, so you should have j = 0; j < n, assuming n is the size of the array.
Then, you assign to smallest every time around the loop, not just if the new value is smaller. So, what you're seeing is the last value.
Assuming that you really do run one past the end of the row, this "last value" is probably actually the first value in the next row. Or some arbitrary value stored in the memory that just so happens to be past the end of the array, if your array has exactly 2 rows. Anyway, it's Undefined Behavior to read past the end of an array, which is Not Good. Anything is allowed to happen, and what does happen often is more puzzling than you expect.
Careful about your array indexes. In C array indexes start at zero.
For:
double array[10];
you would go through all ten elements with:
int i;
for( i = 0; i < 10; i++ )
printf( "The array value at %d is %g\n", i, array[i] );