|
Page 1 of 1 |
|
Posted: Sat, 19th Nov 2011 16:41 Post subject: Beginner C question |
|
 |
hey folks, I was hoping you could guys could help me out with this assignment. Basicly what I have to do is user has to enter numbers into 2 dimensional array and the program is supposed to print out the largest most common number and its index. I've got this far and now am kinda stuck trying to get indexes to the numbers in array, so it would be awesome if you guys could help out and explain how it is done. Here's where I am so far.
Thanks in advance
Code: |
#include <stdio.h>
int main(void)
{
int N, M;
printf("Enter desired rows\n");
printf("Desired rows: ");
scanf("%d", &N);
printf("\n");
printf("Enter desired columns (Number must be between 1 and 10)\n");
printf("\n");
printf("Desired columns: ");
do{
scanf("%d", &M);
if(M<1 || M>10){
printf("Entered number is incorrect, please try again.\n Number is supposed to be between 1 and 10.");
}
else if(M>=1 || M<=10){
printf("You entered numbers correctly!\n");
}
}
while(M<1 || M>10);
printf("\n Enter numbers into array: ");
int A[N][M], i, j;
for(i=0;i<N;i++){
for(j=0;j<M;j++){
scanf("%d", &A[i][j]);
}
}
for(i=0;i<N;i++){
for(j=0;j<M;j++){
printf("%d ", A[i][j]);}
printf("\n");
}
getch();
return 0;
}
|
|
|
Back to top |
|
 |
Frant
King's Bounty
Posts: 24433
Location: Your Mom
|
|
Back to top |
|
 |
|
Posted: Sun, 20th Nov 2011 20:39 Post subject: |
|
 |
Okey, so I managed to get indexes before the entered numbers but how to get the most common number in the array with its index to print on screen is beyond me, so some help with that would really be appriciated (Example as well: user enters numbers 4 6 3 7 6 4 2 6 9 so it is supposed to print that number 6 is common number and it was entered as 2nd 5th and 7th number) Here's the code so far.
Code: | #include <stdio.h>
int main(void)
{
int N, M;
printf("Enter desired rows\n");
printf("Desired rows: ");
scanf("%d", &N);
printf("\n");
printf("Enter desired columns (Number must be between 1 and 10)\n");
printf("Desired columns: ");
do{
scanf("%d", &M);
if(M<1 || M>10){
printf(""Entered number is incorrect, please try again.\n Number is supposed to be between 1 and 10\n\n.");
}
else if(M>=1 || M<=10){
printf("You entered numbers correctly!\n");
}
}
while(M<1 || M>10);
int A[N*M][2], i;
for(i=0;i<N*M;i++){
A[i][0]=i+1;
printf("Enter numbers into array %d: ", i+1);
scanf("%d", &A[i][1]);
}
for(i=0;i<N*M;i++){
printf("%d - %d\n", A[i][0], A[i][1]);
}
getch();
return 0;
}
|
thanks in forward
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
[mrt]
[Admin] Code Monkey
Posts: 1338
|
Posted: Sat, 26th Nov 2011 01:03 Post subject: |
|
 |
C or C++?
C++ has a nice little STL library that houses map (an associative array).
Otherwise you have to reinvent the wheel, which is a bummer. I'm alittle late so if you still need a hand post a cry for help.
teey
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
Veki
Posts: 380
Location: Croatia
|
Posted: Tue, 28th Feb 2012 12:30 Post subject: |
|
 |
Code: |
struct database{
char namei;
int age;
float salary;
};
|
You have only 1 character for name. Don't you need a char array to store complete name?
for example char[128] - a char array for 128 characters
Code: |
for(int j=0;j<strlen(tahemark);j++){
if(tahemark[j]==" "){
k++;
}
switch(k){
case 0:
andmed[i].name=tahemark[j];
break;
case 1:
andmed[i].age=tahemark[j];
break;
case 2:
andmed[i].salary=tahemark[j];
break;
}
}
|
This will not work.
Case 0 is constantly overwriting name variable if name is longer than 1 character.
Case 1 will write ASCII code in integer variable.
Case 2 will write ASCII code in float variable.
If you know the format your variable are in text file you should use is fscanf.
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/
Beware of he who would deny you access to information, for in his heart he dreams himself your master.
Commisssioner Pravin Lal
"U.N. Declaration of Rights"
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group
|
|
 |
|