d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Totally Stumped By This C Programming Assignment
Add Reply New Topic New Poll
Member
Posts: 29,400
Joined: Mar 27 2008
Gold: 504.69
Dec 2 2014 02:28pm
Quote
Write a C Program to read five numbers a,b,c,d,e and have a function Repeatednumber returns back the repeated numbers. Ex: if the numbers a=1, b=2, c=3, d=1, e=2 the function will print (1 and 2).


What I got so far but still doesn't work :(
I haven't put it as a function because I can't even get it to work without one.
A couple scenarios that are stumping is determining how many unique repeated numbers there are, or if the user enters 5 of the same number and only printing the one number.

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

void main(void){

int num[5], repeated[2]={0}, i, j;

//read five integers
for (i=0;i<5;i++){
printf("Enter an integer: ");
scanf("%d", &num[i]);
}

for(i=0;i<=4;i++){
for(j=i+1;j<=4;j++){
if (num[i]==num[j]){
printf("%d\t", num[i]);
}
}
}


This post was edited by ROM on Dec 2 2014 02:30pm
Member
Posts: 29,400
Joined: Mar 27 2008
Gold: 504.69
Dec 2 2014 03:22pm
Update.

Got it to do what I wanted without a function but it seems more complicated then it needs to be.

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

void main(void){

int num[5], repeated[2]={0}, i, j, k=0;

//read five integers
for (i=0;i<5;i++){
printf("Enter an integer: ");
scanf("%d", &num[i]);
}

for(i=0;i<=4;i++){
for(j=i+1;j<=4;j++){
if (num[i]==num[j]){
if(num[i]==repeated[0]){
continue;
}
repeated[k] = num[i];
k=1;
}
}
}

if(repeated[0]==0&&repeated[1]==0){
printf("There are no repeating numbers.");
}
else if(repeated[0]!=0&&repeated[1]==0){
printf("%d", repeated[0]);
}
else{
printf("%d and %d", repeated[0], repeated[1]);
}

printf("\n");
system("pause");
}
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Dec 2 2014 03:46pm
Code
#include <stdio.h>
#include <stdlib.h>

void main(void){

int num[5], i, j, k=0;
int temp = 0;

//read five integers
for (i=0;i<5;i++){
printf("Enter an integer: ");
scanf("%d", temp); //or however you would store it in temp

for (int j=0; j < i; j++) {
if (temp == num[j]) {
print temp
return;
}
}

num[i] = temp;
}
}


basically when you read in the new number, store it in some temp variable and then iterate through your array to see if it exists. If you find it, print out the number and your done. Otherwise you store it in the i'th index

O(n^2) if that means anything to you. I'm sure it can be done faster but you're probably not concerned with that

This post was edited by Aimed_Shot on Dec 2 2014 03:50pm
Member
Posts: 29,400
Joined: Mar 27 2008
Gold: 504.69
Dec 2 2014 04:21pm
Quote (Aimed_Shot @ Dec 2 2014 05:46pm)
Code
#include <stdio.h>
#include <stdlib.h>

void main(void){

int num[5], i, j, k=0;
int temp = 0;

//read five integers
for (i=0;i<5;i++){
  printf("Enter an integer: ");
  scanf("%d", temp); //or however you would store it in temp

  for (int j=0; j < i; j++) {
            if (temp == num[j]) {
  print temp
  return;
      }
  }
 
  num[i] = temp;
}
}


basically when you read in the new number, store it in some temp variable and then iterate through your array to see if it exists. If you find it, print out the number and your done. Otherwise you store it in the i'th index

O(n^2) if that means anything to you. I'm sure it can be done faster but you're probably not concerned with that


A couple problems. I wouldn't be able to isolate it as a function and in the scenario the user enters 1, 1, 1, 1, and 1 it would print 1 four times, even though 1 is only number repeated.
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Dec 2 2014 04:33pm
Quote (ROM @ Dec 2 2014 04:21pm)
A couple problems. I wouldn't be able to isolate it as a function and in the scenario the user enters 1, 1, 1, 1, and 1 it would print 1 four times, even though 1 is only number repeated.


It would only print it once. Notice the return statement


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

void main(void){

int num[5], i, j, k=0;
int temp = 0;

//read five integers
for (i=0;i<5;i++){
printf("Enter an integer: ");
scanf("%d", temp);

if (repeatedNumber(temp, i, &num)) {
printf("Repeated number: %i", temp);
break;
}
}

}

bool repeatedNumber(temp, i, int *num[]) {

for (int j=0; j < i; j++) {
if (temp == num[j])
return true;
}

num[i] = temp;
return false;
}


This post was edited by Aimed_Shot on Dec 2 2014 04:36pm
Member
Posts: 29,400
Joined: Mar 27 2008
Gold: 504.69
Dec 2 2014 04:38pm
Quote (Aimed_Shot @ Dec 2 2014 06:33pm)
It would only print it once. Notice the return statement


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

void main(void){

int num[5], i, j, k=0;
int temp = 0;

//read five integers
for (i=0;i<5;i++){
    printf("Enter an integer: ");
    scanf("%d", temp);
 
    if (repeatedNumber(temp, i, &num))  {
                printf("Repeated number: %i", temp);
                break;
            }
}

}

bool repeatedNumber(temp, i, int *num[]) {

    for (int j=0; j < i; j++) {
        if (temp == num[j])
    return true;
    }
 
    num[i] = temp;
    return false;   
}


Interesting. I will have to check it out some more and try to understand it. :lol:
This is what I got and it works as well. :P

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

void Repeatednumber(int num[], int repeated[]);

void main(void){

int num[5], repeated[2]={0}, i;

for (i=0;i<5;i++){
printf("Enter an integer: ");
scanf("%d", &num[i]);
}

printf("\nRepeated Numbers: ");
Repeatednumber(num, repeated);

printf("\n");
system("pause");
}

void Repeatednumber(int num[],int repeated[]){
int i, j, k=0;

for(i=0;i<=4;i++){
for(j=i+1;j<=4;j++){
if (num[i]==num[j]){
if(num[i]==repeated[0]){
continue;
}
repeated[k] = num[i];
k=1;
}
}
}

if(repeated[0]==0&&repeated[1]==0){
printf("There are no repeating numbers.");
}
else if(repeated[0]!=0&&repeated[1]==0){
printf("%d", repeated[0]);
}
else{
printf("%d and %d", repeated[0], repeated[1]);
}
}


Go Back To Programming & Development Topic List
Add Reply New Topic New Poll