I was doing a Bingo - type project when i started to get some errors in my first "for" loop and in the end of the "bingo" structure.
Can someone tell me what have I done wrong?
struct bingo{
int table[5][5];
int i;
int j;
for (i = 0; i < 25; i++)
{
*(table + i) = rand()%75 + 1;
for (j = 0; j < 25; j++)
{
if ((j != i) && ((*(table + i)) == (*(table + j))))
{
i--;
j = 5 * 5;
}
}
}
};
errors:
1 IntelliSense: expected a type specifier ( first for)
2 IntelliSense: expected an identifier (end of the sturct)
Structs are only able to contain variables, they can't have any logic in them. In C++ you can have functions defined in structs, but even then you need a function header and body.
Related
I'm new to C and learning it for a class right now. We are currently working on a little project and we are supposed to use pointer arithmetic to access arrays as opposed to the standard [] way.
For some reason, I can use it just fine on the first loop (see code) but when I use it in the second, it doesn't produce the same outcome as if I were to use the standard [] way.
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int num = *(*array+i)+j;
//Irrelevant code
}
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int num = array[j][i]; // Error comes if I do *(*array+j)+i;
//Irrelevant code
}
}
I don't know if I am missing something here but why would calling the array using pointer arithmetic be different between the 2 loops?
The equivalence between subscripts and pointer notation is:
a[i] == *(a + i)
You are using (*a + i) in place of the correct *(a + i).
I believe your first set of loops should read:
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int spot = *(*(board+i)+j);
for (int k = j + 1; k < size; k++) {
if (spot == *(*(board + i) + k) && spot > 0) {
return 0;
}
}
}
}
However, since you've not provided an MCVE (Minimal, Complete, Verifiable Example
— or MRE or whatever name SO now uses)
or an
SSCCE (Short, Self-Contained, Correct Example
— the same idea by a different name), I can't easily test the code.
Also, now you know why it is better to use the explicit subscript notation; it is a lot harder to get it wrong.
The module that swaps row to make an array sorted in ascii ascending order keeps returning error.
const int MAX = 10;
const int MAX_STR = 80;
void asciiOrder(char (*buffer)[MAX_STR]);
void asciiOrder(char (*buffer)[MAX_STR]) {
char * temp;
for (int i = 0; i < sizeof(buffer) / sizeof (buffer[0]) - 1; i++) {
for (int j = 1; i + j < sizeof(buffer) / sizeof(buffer[0]); j++) {
for (int k = 0; k < strlen(buffer[i]) && buffer[i][k] != NULL; k++) {
if (buffer[i][k] > buffer[i+j][k]) {
temp = buffer[i+j];
buffer[i+j] = buffer[i];
buffer[i] = temp;
break;
}
}
}
}
}
And this is the error I got from the entire code:
practice102.c:87:23: error: assignment to expression with array type
buffer[i+j] = buffer[i];
^
practice102.c:88:21: error: assignment to expression with array type
buffer[i] = temp;
I searched other posts but still don't know why.
How to solve this problem?
C forbidden to assign a 2D array without specify the two index.
You have to write something like
buffer[a][b] = buffer[c][d]
whatever your a,b,c,d are.
Also be sure that your buffer temp are filled and/or allocated.
Actually I want to bubble sort entire records with respect to name(in Alphabetical order). but compiler gives error of "invalid array assignment".
please check the code given below, especially the bubblesort part.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct form {
char name[20], employ[20], gend[20], email[20], father[20];
int num, serial;
} a[11];
main() {
int c, tmp, d;
for (c = 0; c < 10; c++) {
printf("Employ#:");
scanf("%d", &a[c].serial);
printf("Enter Name:");
scanf("%s", &a[c].name);
printf("\n");
printf("Enter Father Name:");
scanf("%s", &a[c].father);
printf("\n");
printf("Enter Email.Id:");
scanf("%s", &a[c].email);
printf("\n");
printf("Enter employment#:");
scanf("%s", &a[c].employ);
printf("\n");
printf("Enter Gender:");
scanf("%s", &a[c].gend);
printf("\n");
printf("Enter Number:");
scanf("%d", &a[c].num);
printf("\n");
}
//Bubblesorting
int i, j, temp;
for (j = 0; j < 10; j++) {
for (i = 0; i < 10; i++) {
if (a[i].name[0] < a[i + 1].name[0]) {
a[10].serial = a[i].serial;
a[10].name = a[i].name;
a[10].father = a[i].father;
a[10].email = a[i].email;
a[10].employ = a[i].employ;
a[10].gend = a[i].gend;
a[10].num = a[i].num;
a[i].serial = a[i + 1].serial;
a[i].name = a[i + 1].name;
a[i].father = a[i + 1].father;
a[i].email = a[i + 1].email;
a[i].employ = a[i + 1].employ;
a[i].gend = a[i + 1].gend;
a[i].num = a[i + 1].num;
a[i + 1].serial = a[10].serial;
a[i + 1].name = a[10].name;
a[i + 1].father = a[10].father;
a[i + 1].email = a[10].email;
a[i + 1].employ = a[10].employ;
a[i + 1].gend = a[10].gend;
a[i + 1].num = a[10].num;
}
}
}
system("cls");
printf("\nEmployee#\tName\tFather Name\tEmail\t\t\a[10]loyement#\tGender\tContact#");
for (c = 0; c < 10; c++) {
printf("\n\t%d\t", a[c].serial);
printf("%s\t", a[c].name);
printf(" %s\t", a[c].father);
printf("%s\t", a[c].email);
printf("\t%s\t", a[c].employ);
printf("%s\t", a[c].gend);
printf("%d\t", a[c].num);
}
getch();
}
The reason you get the error is because, you are trying to copy array of characters using the assignment operator.
For example ::
int a[10], b[10];
b = a;
is a wrong way to assign a to b, which you are doing everywhere in your code, which is giving the error "invalid array assignment".
Moreover, I do not understand why you are doing so much hardwork in copying all the variable of the struct like this, simply doing this would help as well.
for (j = 0; j < 10; j++) {
for (i = 0; i < 10; i++) {
if (a[i].name[0] < a[i + 1].name[0]) {
a[10] = a[i];
a[i] = a[i+1];
a[i+1] = a[10];
}
}
}
Moreover, you compare the 2 structs, based on the 1st character of their name[]. So, in case you have 2 names like "ABD" and "ACD" in your a array, they do not get sorted, so you shall try modifying your criteria on which you sort 2 objects.
First, your bubble sort looks nothing like one that you would find in any documentation concerning sorting. It should be a very simple doubly-nested loop and comparing two items in the inner loop and swapping them if they're out of order.
Second, to sort alphabetically, you're supposed to compare the entire string, not just the first letter. Since you didn't pick a language, let's assume it is C for the moment. You would use strcmp to compare C-style strings.
Last, you don't need to swap one item in your struct at a time. You can swap the entire struct.
#include <string.h>
//...
int i, j, temp;
int num_items = 10;
for (j = 0; j < num_items - 1; j++)
{
for (i = 0; i < num_items - 1; i++)
{
if ( strcmp(a[i].name, a[i + 1].name ) > 0)
{
form temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
Also, there is no need to make your array artificially have more items than necessary. You declared your array as having 11 items, and I guess the reason was to make the bubble sort work.
There is no reason to declare 11 items. Just declare it as 10 items and write the bubble sort correctly (note that the difference in the loop constraints). What if the form struct was very large? You would have declared 1 more item than necessary, using memory for no reason whatsoever.
I'm very new to the C programming language and Stack Exchange. I'm learning on my own and have recently refined a rudimentary skill set in Matlab. I'm attempting to mess around with arrays in the following C code sample, which I hope I have adequately explained throughout. When I compile the code, however, I'm immediately met with a segmentation fault. I'm not sure where I've hit my iceberg, and I'm looking for a solution so I can learn by good example.
/* colAdd takes a 4,4 array 'A' and adds columns 'C1' and 'C2',
placing the result in column 'C1'. Prints matrix 'A'.
Void function as result is only printed, not returned.
Main tests colAdd with simple 4,4 array */
#include <stdio.h>
// Function colAdd is declared
void colAdd(int A[4][4], int C1, int C2);
int main()
{
// Function colAdd is initialised
int A[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
colAdd(A,1,3);
}
void colAdd(int A[4][4], int C1, int C2)
{
int j = 0; int i; int arr1[4]; int arr2[4]; int arr3[4];
// The following takes column C1 of A and places the result in arr1
for(i = (C1 - 1); i = (C1 + 11); i + 4) {
arr1[j] = A[C1][i];
j = j + 1;
}
j = 0;
// The following takes column C2 of A and places the result in arr2
for(i = (C2 - 1); i = (C2 + 11); i + 4) {
arr2[j] = A[C2][i];
j = j + 1;
}
// The following takes the addition of arr1 and arr2 and places the result in arr3
for(i = 0; i = 3; i++) {
arr3[i] = arr2[i] + arr1[i];
}
j = 0;
// The following replaces column C1 of array A with arr3
for(i = (C1 - 1); i = (C1 + 11); i + 4) {
A[C1][i] = arr3[j];
j = j + 1;
}
//Finally, the resultant array A is printed
const int rows = 4;
const int cols = 4;
int k; int l;
for(k = 0; k < rows; k++) {
for(l = 0; l < cols; l++) {
printf("ans[%d, %d] = %d \n", k, l, A[k][l]);
}
}
}
Any advice would be much appreciated. Thank you all kindly.
You have:
// The following takes column C1 of A and places the result in arr1
for(i = (C1 - 1); i = (C1 + 11); i + 4) {
arr1[j] = A[C1][i];
j = j + 1;
}
Your code doesn't do what your comment says it should. Your understanding of for statement is not quite right.
Use:
for(i = 0; // This is executed only once.
i < 4; // This is executed for every iteration of the loop.
// The loop is terminated if this statement is false.
++i) // This is executed for every iteration after the
// statements of the loop are executed.
{
arr1[i] = A[i][C1];
}
Hopefully this gives you enough to fix the rest of the function.
Your array 'A' is a 4x4 2D array, but you access your array's 2nd index using 'i', which will eventually be 12 in the first two loops in your function colAdd(). This will cause an index out of bounds error (or segmentation fault).
I'm guessing 'i' should be 'j' when indexing through the 2D array.
Also you should use "==" for the terminating condition in your for loop, instead of "=" which is assignment. Also how you increment through the loop 'i + 4', won't change 'i', you need a "+=" or do "i = i + 4".
I am compiling a small C program with Microsoft Visual Studio 2010 on Windows 7. Here's a small snippet:
void test(char[] s)
{
//some code here
}
But I am getting the following error:
Expected a ')'
How would I resolve this issue?
Actually the full code is here , which implement radix sort MSD in C (chapter 10 of this book):
#define N // integers to be sorted with values from 0 -256
void MSD (char[] s) {
msd_sort(s, 0, len(s), 0)
}
msd_sort(char [][] s, int lhs, int rhs, int d )
{
if (rhs<=lhs+1) return;
int * count =(int * )malloc(257*sizeof(int));
for(int i = 0; i < N; ++i)
count[s[i][d]+1]++;
for(int k = 1; k < 256; ++k)
count[k] += count[k-1];
for(int j = 0; j < N; ++j)
temp[count[s[i][d]]++] = a[i];
for(int i = 0; i < N; ++i)
s[i] = temp[i];
for(int i = 0; i<255;++i)
msd_sort(s, 1 + count[i], 1 + count[i+1], d+1);
}
void test(char s[]) {}
^^---brackets go here
What you wrote is Java.
Also :
don't forget to give a value to your #define N.
I see one malloc and no free, that's a memory leak.